2016-11-23 85 views
1

我是相当新的节点JS和MongoDB,我需要添加或更新有关MongoDB集合子文档如下:添加和MongoDB中更新子文档和节点JS(不含猫鼬)

{ 
    "_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": "https://pbs.twimg.com/profile_images/566353055788978177/dUy_ueY2.jpeg" 
      } 
     } 
    ] 
} 

所有我找到的解决方案是使用猫鼬,在这个特定的项目中,我们决定不使用猫鼬,任何想法?

回答

3

要添加或更新嵌入文档中的新talk,你可以使用任何原子update operators取决于集合中有多少文件 你想要更新。对于单个原子更新,使用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操作者将追加将指定的文档转换为嵌入文档数组(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(); 
     } 
    ) 

}); 

随着现有文档更新,应用与$ positional operator$set运营商一起在您的更新操作来更改嵌入文档领域。 $ positional operator将识别数组中正确的元素进行更新,而不显式指定数组中元素的位置。对于这项工作,数组字段必须出现在查询文档的一部分,因此查询

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

看看Node.JS MongoDB driver

基本例如

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(); 
     }); 
    }); 
    }); 
+0

谢谢你的澄清,但我不明白到底是什么{A:1},{B:1 }的意思,同样的事情出现在文档https://mongodb.github.io/node-mongodb-native/api-generated/collection.html#update,我真的转储! –

+0

正如我的示例所示,我需要添加新的对话或更新现有的对话。 –