[英]Add and update subdocument in mongoDB and node js (without mongoose)
我对Node js和mongoDB相当陌生,我需要在mongodb集合上添加或更新SubDocument,如下所示:
{
"_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"
}
}
]
}
我发现的所有解决方案都是使用猫鼬,在这个特定项目中,我们决定不使用猫鼬,对此有何想法?
要在嵌入式文档中添加或更新新talk
,可以使用任何原子更新操作符,具体取决于要更新的集合中有多少个文档。 对于单个原子更新,请使用以下示例中的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();
}
)
});
对于现有文档更新,您可以在更新操作中将$set
运算符与$
position运算符一起使用,以更改嵌入的文档字段。 $
位置运算符将标识要更新的正确元素,而无需显式指定元素在数组中的位置。 为此,array字段必须作为查询文档的一部分出现,因此查询
{
"_id": ObjectId("58286e49769e3729e895d239"),
"talk.id": "3" // <-- array field is part of the query
}
基本例子
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();
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.