【发布时间】:2021-04-23 09:39:57
【问题描述】:
注意:检查密钥books是否存在,如果不存在则创建并更新它。
我正在使用带有 nodejs 的 mongodb 驱动程序。
在db.collection('userData')的文件长这样:
{
user_id: 'user1',
books: [{
id: 'book1',
title: 'this is book1'
},
{
id: 'book1',
title: 'this is book1'
}]
}
在插入新书目时,如何检查文档中是否存在books的数组,如果不存在则在文档中添加键books,然后插入书目。
【问题讨论】:
-
我必须检查关键的“书”是否存在?
-
docs.mongodb.com/manual/reference/operator/query/elemMatch
db.scores.find( { results: { $elemMatch: { $gte: 80, $lt: 85 } } } )在元素数组中搜索。也许也适用于 upsert 命令?
标签: node.js mongodb mongodb-nodejs-driver