Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更新Mongodb中的多嵌套数组_Mongodb - Fatal编程技术网

更新Mongodb中的多嵌套数组

更新Mongodb中的多嵌套数组,mongodb,Mongodb,我在Mongodb中有一个文档模式,如下所示: { _id: 1 tags: [{ tag: 'foo' links: [{ link: 'http:www.google.com' date: '123' }] }] } 我试图将一个链接推送到“links”数组中,该数组将是文档的唯一链接 我的第一个问题 db.userlinks.update ( {_id: 1,

我在Mongodb中有一个文档模式,如下所示:

{
    _id: 1
    tags: [{
        tag: 'foo'
        links: [{
            link: 'http:www.google.com'
            date: '123'
        }] 
    }]
}
我试图将一个链接推送到“links”数组中,该数组将是文档的唯一链接

我的第一个问题

db.userlinks.update (
    {_id: 1, tags: {$nin: [{tag:'foo'}]}}, 
    {$push: {'tags': {tag:'foo'}}}, 
    {upsert: true}
)
给我这个(如果不存在,则创建标记)

然后我会继续问这个问题

db.userlinks.update (
    {_id: 1, tags: {tag: 'foo', links: {$nin: [{link: 'http://www.google.com'}]}}}, 
    {$push: {tags: {tag: 'foo', links: {link: 'http://www.google.com', date: '123'}}}}, 
    {upsert: true}
)
但我得到了这个错误:“无法将$push/$pushAll修饰符应用于非数组”

我很确定问题出在我的第二个查询的“更新”组件中,但我不确定如何修复它。任何帮助都将不胜感激

编辑 我的第一个问题是。。。(感谢乔)

我的第二个问题是

db.userlinks.update (
    {_id: 1, 'tags.tag': 'foo'}, 
    {$push: {'tags.$.links': {link: 'http://www.google.com', date: '123'} } }
)
它成功地将链接推送到“链接”数组中,但也允许重复。我不允许重复链接$addToSet的工作类型,但如果日期更改,则仍会插入重复链接


有没有办法在第二次查询的“查询”部分检查链接是否存在,或者在某些字段匹配时只添加设置?

也许可以将第一次查询更改为:

db.userlinks.update (
    {_id: 1, tags: {$nin: [{tag:'foo'}]}}, 
    {$push: {'tags': {tag:'foo', links:[]}}}, 
    {upsert: true}
)
$push操作应该只影响链接,而不影响标记

{$push: {'tags.links': {link: 'http://www.google.com', date: '123'} } },

我终于明白了。。。尽管如果有人能找到更好的方法,请添加它作为答案

// create the userlinks collection if it doesn't exist
// also add a tag 'foo' into it, but only if the tag doesn't exist
db.userlinks.update (
    {_id: '1', 'tags.tag': {$nin: ['foo']}}, 
    {$push: {'tags': {tag:'foo', links:[]}}},
    {upsert: true}
)

// add a link into the 'foo' tag, but only if the link doesn't exist
db.userlinks.update(
    {_id: '1', 'tags.tag': 'foo', 'tags.links.link': {$nin: ['http://foobar.com']}},
    {$push: {'tags.$.links': {link: 'http://foobar.com', date: '15'} } }
)

这启动了指向空数组的“链接”,这很好,可能是我所需要的,但是我的第二个查询仍然会引发相同的错误。不幸的是,我在使用第二个查询时仍然有相同的错误:db.userlinks.update({u id:1,tags:{tag:'foo',links:{$nin:[{link:'google com'}]}}},{$push:{'tags.links':{链接:'google.com',日期:'123'}},{upsert:true})你不能。考虑改变你的模式吗?我并不特别想改变我的模式,因为它被精确地模仿了它的输出方式,这意味着一旦我从数据库中取出数据,我就不必做任何数据争用。我认为这应该是使用MangGDB的主要原因。o和数据库。@Harley:如果我们知道{u id:,'tags.tags'和'tags.links.link'…db.doc.update({{u id:,'tags.tags:,'tags.links.link':},{$inc:{'tags.links.$.date:'20}),我们怎么能将日期更新到20因为我们有两个数组,所以无法工作。请帮助抱歉,Praveen,自从我发表这篇文章以来,我没有与Mongo合作过,我恐怕已经基本忘记了查询语言的复杂性。也许可以提出一个新问题?
{$push: {'tags.links': {link: 'http://www.google.com', date: '123'} } },
// create the userlinks collection if it doesn't exist
// also add a tag 'foo' into it, but only if the tag doesn't exist
db.userlinks.update (
    {_id: '1', 'tags.tag': {$nin: ['foo']}}, 
    {$push: {'tags': {tag:'foo', links:[]}}},
    {upsert: true}
)

// add a link into the 'foo' tag, but only if the link doesn't exist
db.userlinks.update(
    {_id: '1', 'tags.tag': 'foo', 'tags.links.link': {$nin: ['http://foobar.com']}},
    {$push: {'tags.$.links': {link: 'http://foobar.com', date: '15'} } }
)