protected virtual int DoUpdate(Connector.Database db, Doc row, IDataStoreKey key, FieldFilterFunc filter = null) { var doc = convertDocToBSONDocumentWith_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); }
/// <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(); } }