예제 #1
0
파일: Collection.cs 프로젝트: uzbekdev1/nfx
        /// <summary>
        /// Updates or inserts 1 document on the server. Inspect CRUDResult for write errors and docs affected/matched
        /// </summary>
        public CRUDResult Save(BSONDocument document)
        {
            EnsureObjectNotDisposed();

            if (document == null)
            {
                throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.Save(document==null)");
            }

            var _id = document[Protocol._ID];

            if (_id == null || _id is BSONNullElement)
            {
                throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.Save(document._id absent|null)");
            }

            var connection = Server.AcquireConnection();

            try
            {
                var reqId   = Database.NextRequestID;
                var updates = new UpdateEntry[] { new UpdateEntry(
                                                      new Query("{'" + Protocol._ID + "': '$$ID'}", true, new TemplateArg("ID", _id.ElementType, _id.ObjectValue)),
                                                      document,
                                                      multi: false,
                                                      upsert: true
                                                      ) };

                return(connection.Update(reqId, this, updates));
            }
            finally
            {
                connection.Release();
            }
        }
예제 #2
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key, FieldFilterFunc filter = null)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "update", filter);
            var _id = doc[Connector.Protocol._ID];

            doc.Delete(Connector.Protocol._ID);
            if (doc.Count == 0)
            {
                return(0);              // nothing to update
            }
            //20160212 spol
            if (filter != null)
            {
                var wrapDoc = new BSONDocument();
                wrapDoc.Set(new BSONDocumentElement(Connector.Protocol.SET, doc));
                doc = wrapDoc;
            }

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(_id);
            var upd = new Connector.UpdateEntry(qry, doc, false, false);

            var result = collection.Update(upd);

            CheckCRUDResult(result, row.Schema.Name, "update");

            return(result.TotalDocumentsAffected);
        }
예제 #3
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "update");

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(doc[Connector.Protocol._ID]);
            var upd = new Connector.UpdateEntry(qry, doc, false, false);

            var result = collection.Update(upd);

            checkCRUDResult(result, row.Schema.Name, "update");

            return(result.TotalDocumentsAffected);
        }
예제 #4
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key)
        {
          var doc = convertRowToBSONDocumentWith_ID(row, "update");
          
          var tname = GetCollectionName(row.Schema);
          
          var collection = db[tname]; 
          
          var qry = new Connector.Query();
          qry.Set( doc[Connector.Protocol._ID] );
          var upd = new Connector.UpdateEntry(qry, doc, false, false);

          var result = collection.Update( upd );

          checkCRUDResult(result, row.Schema.Name, "update");

          return result.TotalDocumentsAffected;
        }
예제 #5
0
        /// <summary>
        /// Updates or inserts 1 document on the server. Inspect CRUDResult for write errors and docs affected/matched
        /// </summary>
        public CRUDResult Save(BSONDocument document)
        {
          EnsureObjectNotDisposed();

          if (document==null)
            throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR+"Collection.Save(document==null)");

          var _id = document[Protocol._ID];
          if (_id==null || _id is BSONNullElement)
            throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR+"Collection.Save(document._id absent|null)");

          var connection = Server.AcquireConnection();
          try
          {
            var reqId = Database.NextRequestID;
            var updates = new UpdateEntry[]{ new UpdateEntry(
                   new Query("{'"+Protocol._ID+"': '$$ID'}", true, new TemplateArg("ID", _id.ElementType, _id.ObjectValue)),
                   document,
                   multi: false,
                   upsert: true
            ) };
            
            return connection.Update(reqId, this, updates);
          }
          finally
          {
            connection.Release();
          }
        }
예제 #6
0
        internal CRUDResult Update(int requestID, Collection collection, UpdateEntry[] updates)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_UPDATE(m_BufferStream, requestID, collection, updates);
          writeSocket(total);
          var got = readSocket();
          return Protocol.Read_CRUD_Response("Update", got);
        }
예제 #7
0
파일: Protocol.cs 프로젝트: itadapter/nfx
        public static Int32 Write_UPDATE(Stream stream,
            Int32 requestID,
            Collection collection,
            UpdateEntry[] updates)
        {
            var body = new BSONDocument();
                          body.Set( new BSONStringElement("update", collection.Name) );

                          var writeConcern = getWriteConcern(collection);
                          if (writeConcern!=null)
                            body.Set( new BSONDocumentElement("writeConcern", writeConcern) );

                          var arr = updates.Select( one =>
                                                    {
                                                      var doc = new BSONDocument();
                                                      doc.Set( new BSONDocumentElement("q", one.Query) );
                                                      doc.Set( new BSONDocumentElement("u", one.Update) );

                                                      if (one.Multi)
                                                        doc.Set( new BSONBooleanElement("multi", true) );

                                                      if (one.Upsert)
                                                        doc.Set( new BSONBooleanElement("upsert", true) );

                                                      return new BSONDocumentElement(doc);
                                                    }
                          ).ToArray();

                          body.Set( new BSONArrayElement("updates", arr) );

                          return Write_QUERY(stream, requestID, collection.Database, null, QueryFlags.None, 0, -1, body, null);
        }
예제 #8
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key, FieldFilterFunc filter = null)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "update", filter);
              var _id = doc[Connector.Protocol._ID];

              //20160212 spol
              if (filter != null)
              {
            doc.Delete(Connector.Protocol._ID);
            if (doc.Count == 0) return 0; // nothing to update
            var wrapDoc = new BSONDocument();
            wrapDoc.Set(new BSONDocumentElement(Connector.Protocol.SET, doc));
            doc = wrapDoc;
              }

              var tname = GetCollectionName(row.Schema);

              var collection = db[tname];

              var qry = new Connector.Query();
              qry.Set( _id );
              var upd = new Connector.UpdateEntry(qry, doc, false, false);

              var result = collection.Update( upd );

              checkCRUDResult(result, row.Schema.Name, "update");

              return result.TotalDocumentsAffected;
        }