另一个库可能会使这种类型的操作更容易https://www.npmjs.com/package/@coolgk/mongo
文档中的示例:
原始数据:
{
_id: '5a8c16f3c452fd2c0d3687c6',
_dateModified: '2018-02-20T12:39:15.258Z', // auto generated
title: 'Support Ticket 1',
messages: [{
_id: '5a8c16f3c452fd2c0d3687c9', // auto generated
user: 'customer',
message: 'I found a bug',
_dateModified: '2018-02-20T12:39:15.259Z' // auto generated
},
{
_id: '5a8c16f3c452fd2c0d3687c8', // auto generated
user: 'support',
message: 'Restart your computer',
_dateModified: '2018-02-20T12:39:15.259Z' // auto generated
},
{
_id: '5a8c16f3c452fd2c0d3687c7', // auto generated
user: 'developer',
message: 'That\'s not a bug, it\'s a feature',
_dateModified: '2018-02-20T12:39:15.259Z' // auto generated
}
]
}
更新子文档
下面的脚本将更新数组"message"
中第二个文档的字段值。"messages"
model.updateOne({
_id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
messages: [
{
_id: '5a8c16f3c452fd2c0d3687c8', // find the document by _id in the array
message: 'Turn on your computer' // update only the message field, other fields will not change
}
]
});
集合中的数据变为
{
_id: '5a8c16f3c452fd2c0d3687c6',
title: 'Support Ticket 1',
messages: [{
_id: '5a8c16f3c452fd2c0d3687c9',
user: 'customer',
message: 'I found a bug',
_dateModified: '2018-02-20T12:39:15.259Z',
},
{
_id: '5a8c16f3c452fd2c0d3687c8',
user: 'support',
message: 'Turn on your computer', // new value
_dateModified: '2018-02-20T12:53:55.890Z' // new modified date
},
{
_id: '5a8c16f3c452fd2c0d3687c7',
user: 'developer',
message: 'That\'s not a bug, it\'s a feature',
_dateModified: '2018-02-20T12:39:15.259Z'
}
],
_dateModified: '2018-02-20T12:53:55.889Z' // new modified date
}
删除文档
下面的脚本将删除"messages"
数组中的第二个文档。
model.updateOne({
_id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
messages: {
$delete: [ '5a8c16f3c452fd2c0d3687c8' ] // $delete operator: delete by _id
}
});
集合中的数据变为
{
_id: '5a8c16f3c452fd2c0d3687c6',
title: 'Support Ticket 1',
messages: [{
_id: '5a8c16f3c452fd2c0d3687c9',
user: 'customer',
message: 'I found a bug',
_dateModified: '2018-02-20T12:39:15.259Z'
},
{
_id: '5a8c16f3c452fd2c0d3687c7',
user: 'developer',
message: 'That\'s not a bug, it\'s a feature',
_dateModified: '2018-02-20T12:39:15.259Z'
}
],
_dateModified: '2018-02-20T12:59:05.602Z' // new modified date
}
全部替换或删除
model.updateOne({
_id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
messages: {
$replace: [] // replace the entire array with a new value
}
});
添加子文档
类似于更新,但_id
在子文档中没有。下面的脚本会将一个新文档添加到messages
数组中。
model.updateOne({
_id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
messages: [
{ // documents without _id = insert new
user: 'Support',
message: 'Please Ctrl + F5'
}
]
});
集合中的数据变为
{
_id: '5a8c16f3c452fd2c0d3687c6',
title: 'Support Ticket 1',
messages: [{
_id: '5a8c16f3c452fd2c0d3687c9',
user: 'customer',
message: 'I found a bug',
_dateModified: '2018-02-20T12:39:15.259Z'
},
{
_id: '5a8c16f3c452fd2c0d3687c7',
user: 'developer',
message: 'That\'s not a bug, it\'s a feature',
_dateModified: '2018-02-20T12:39:15.259Z'
},
{ // new document
_id: '5a8c1d6b082a652c35eb17d6', // auto generated
user: 'Support',
message: 'Please Ctrl + F5',
_dateModified: '2018-02-20T13:06:51.244Z' // auto generated
}
],
_dateModified: '2018-02-20T13:06:51.243Z' // new modified date
}
多重操作
添加、更新和删除可以在一个查询中进行。
model.updateOne({
_id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
messages: {
$update: [
{ // doc contains _id value, this is an update
_id: '5a8c1d6b082a652c35eb17d6', // find the document by _id in the array
message: 'Clear Your Cache' // update only the message field, other fields will not change
},
{ // doc has no _id value, this is an insert
user: 'developer',
message: 'cannot replicate, not a bug!'
}
],
$delete: [ '5a8c16f3c452fd2c0d3687c9' ] // delete by _id (the first doc)
}
});
最后结果
{
_id: '5a8c16f3c452fd2c0d3687c6',
title: 'Support Ticket 1',
messages: [{
_id: '5a8c16f3c452fd2c0d3687c7',
user: 'developer',
message: 'That\'s not a bug, it\'s a feature',
_dateModified: '2018-02-20T12:39:15.259Z'
},
{
_id: '5a8c1d6b082a652c35eb17d6',
user: 'Support',
message: 'Clear Your Cache', // new value
_dateModified: '2018-02-20T13:30:07.123Z' // new modified date
},
{ // new doc
_id: '5a8c22df4656722c3fd787fa',// auto generated
user: 'developer',
message: 'cannot replicate, not a bug!',
_dateModified: '2018-02-20T13:30:07.123Z' // auto generated
}
],
_dateModified: '2018-02-20T13:30:07.121Z' // new modified date
}