如果子文档值不存在,Mongo DB将插入到子文档中 [英] Mongo DB insert into a sub document if sub document value doesn't exist
问题描述
我对mongodb很新,我有点失落。
I'm very new to mongodb and i'm a little lost.
我有mongo db集合,如下所示:
I have mongo db collection that looks like this :
({
_id:id ,
createdAt: new Date(),
name:name,
friends : [{name:1,children:[{name:sarah,age:12}]}],
dogs : [{}]
});
如果名称不存在,我希望能够在friends数组中插入新元素新数组的子元素。
I would like to be able to insert a new element in the friends array if the name doesnt exist and a children element to that new array.
伪代码
如果目录.find id.friends.name = true
更新并添加一个新的子文档{name:sarah ,年龄:5}
else
使用新的子子文档创建一个新的朋友元素
经过大量研究后,似乎人们推荐使用$存在,但我找不到在子文档中使用它的方法'
After alot of research it seems that people are recommending the use of $exist but i can't find a way to use it in a sub document'
我已经想出了这个,但它不是真的很有效,我想知道我怎么能用它作为我的if else stament:
I've come up with this but it's not really working and i'm wondering how could i use that for my if else stament:
db.Directory.find({_id:id},{friends.name:"foo", {"$exists": True}})
对于实际查询
db.Directory.update(
{ _id: id, 'friends.name': "foo" },
{$push: {'friends.$.children': {name:"x",age:"yy"}}}
)
如果它不存在:
db.Directory.insert(
{ _id: id, 'friends.name': "foo" },
{$push: {'friends.$.children': {name:"x",age:"yy"}}}
)
但它并没有真正起作用,我不确定我的公式是否有什么问题,因为这是研究中的大量试验和错误。
我也不知道是否正确的方法,因为我找不到任何关于子文档上存在的值的东西,并且正计划尝试进行Find()搜索存储该值,然后在JS的结果中测试true或false以进行更新/插入调用。
But it's not really working , i'm not sure if there is something wrong in my formulas as it's a lot of trials and errors from research. I also don't really know if that the proper way to do it as i couldn't find anything on a value existing on sub documents and was planning on trying to do a Find() search store the value and then test for true or false on the result in JS to make either of the update/insert calls.
希望有人可以提供帮助
编辑:
假设我想将{name:john,年龄:15}添加到该朋友姓名:1
Edit : Let's say i want to add {name:john,age:15} to that friend name:1
friends : [{name:1,children:[{name:sarah,age:12}]}]
我希望输出为
friends : [{name:1,children:[{name:sarah,age:12},{name:john,age:15}]}]
或者如果名称1不存在
friends : []
使它输出
friends : [{name:1,children:[{name:john,age:15}]}]
更新示例:
案例一:
friends : []
我添加了新朋友名字josh和一个孩子莎拉,12岁我得到:
i add a new friend name "josh" with one child sarah , age 12 i get:
friends : [{name:"josh",children:[{name:sarah,age:12}]}]
这很完美。
Now i add a new friend name 2 with children tom 15.
没有任何事情发生。
之后,我想给josh添加一个新孩子,时间15岁
after , I want to add a new child to josh , timmy age 15
我得到:
friends : [{name:"josh",children:[{name:timmy,age:12}]}]
莎拉消失了。
推荐答案
请尝试通过 批量操作
,通过 $ addToSet
operator and update fri通过 $ set
<结束/ a>运算符,在您更清楚地提出问题后更新它。
Please try to do it through Bulk operation
, add one new friend through $addToSet
operator and update friend through $set
operator, update it after you making your question more clearly.
var bulk = db.Directory.initializeOrderedBulkOp();
// if `friends` is `[]`, push the empty children firstly through addToSet
bulk.find({_id: id, 'friends.name': {$exists: false}}).updateOne(
{$addToSet: {friends: {name: 1, children: []}});
// if we find the match friend, update this one through `$set`
bulk.find({_id: id, 'friends.children.name': 'john'}).updateOne(
{$set: {'friends.$.children': {name: 'john', age: 22}}});
// if we cannot find match friend, insert the new one through `$addToSet`
bulk.find({_id: id, 'friends.children.name': {$ne: "john"}}).updateOne(
{$addToSet: {'friends.0.children': {name: 'john', age: 12}}});
bulk.execute();
这篇关于如果子文档值不存在,Mongo DB将插入到子文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!