Update multi nested array in Mongodb

前端 未结 2 857
醉酒成梦
醉酒成梦 2021-01-17 10:03

I have a document schema in Mongodb that looks like this:

{
    _id: 1
    tags: [{
        tag: \'foo\'
        links: [{
            link: \'http:www.googl         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 10:20

    Perhaps change your first query to:

    db.userlinks.update (
        {_id: 1, tags: {$nin: [{tag:'foo'}]}}, 
        {$push: {'tags': {tag:'foo', links:[]}}}, 
        {upsert: true}
    )
    

    The $push operation should only affect links, not the tag.

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

提交回复
热议问题