Javascript 在mongoDB和node js中添加和更新子文档(不含mongoose)
我是node js和mongoDB的新手,需要在mongoDB集合中添加或更新一个子文档,如下所示:Javascript 在mongoDB和node js中添加和更新子文档(不含mongoose),javascript,json,node.js,mongodb,Javascript,Json,Node.js,Mongodb,我是node js和mongoDB的新手,需要在mongoDB集合中添加或更新一个子文档,如下所示: { "_id": "58286e49769e3729e895d239", "id": "2", "title": "Session Title", "sessiondate": "2016-02-11T21:00:00.000Z", "startTime": "14:30", "endTime": "16:30", "breakStartT
{
"_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"
}
}
]
}
我找到的所有解决方案都是使用mongoose,在这个特定的项目中,我们决定不使用mongoose,有什么想法吗?看看
基本示例
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();
});
});
});
要在嵌入文档中添加或更新新的
talk
,您可以根据集合中的文档数量使用任意原子
您想要更新。对于单个原子更新,请使用以下示例中的方法:
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();
}
)
});
// 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();
}
)
});
在上面,您可以使用操作符将指定的文档附加到嵌入文档数组中(talks
字段)
2。更新现有子文档
// 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();
}
)
});
// 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();
}
)
});
对于现有文档更新,您可以在更新操作中应用运算符和,以更改嵌入的文档字段。将标识数组中要更新的正确元素,而无需显式指定元素在数组中的位置。要使其工作,数组字段必须作为查询文档的一部分出现,因此查询
{
"_id": ObjectId("58286e49769e3729e895d239"),
"talk.id": "3" // <-- array field is part of the query
}
{
“_id”:ObjectId(“58286e49769e3729e895d239”),
“talk.id”:“3”//谢谢您的澄清,但是我不明白{a:1}、{b:1}的确切含义,文档中也出现了同样的情况,我真的很垃圾!正如我的示例所示,我需要添加一个新的对话或更新现有的对话。