MongoDB的&安培;流星 - 查询推到嵌套数组不工作,没有错误抛出 [英] MongoDB & Meteor - Query to push into nested array does not work, no error thrown

查看:156
本文介绍了MongoDB的&安培;流星 - 查询推到嵌套数组不工作,没有错误抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据推到蒙戈集合里面嵌套数组。我沿着蒙戈文档其次在这里, HTTP://docs.mongodb。组织/手动/参考/运营/更新/位置/ ,但还没有任何运气推入阵。没有错误或抛出异常和语法的看起来的权利......

在这个例子中,我试图更新 buyer.boards 标题为一局的推一个新的字符串到它 idArr 阵列。是不是有什么毛病我查询?

蒙戈集合

  //用户从Meteor.users集合文档:
{
    _id:userIdqwerty',
    买家:{
        主板:
            {
                标题:董事会一号,
                idArr:['id123,id456,id678]
            },
            {
                标题:二板,
                idArr:['IDABC']
            },
            {
                标题:'三板',
                idArr:['id12345678','idqwertyuu']
            },
        ]
    };
}

的JavaScript

  VAR选项= {
    boardTitle:董事会一号,
    NEWID:idZjodFsp',
    用户名:userIdqwerty
};Meteor.users.update(
    {
        _id:options.userId,
        '。$ buyer.boards称号。':options.boardTitle
    },
    {$推:{
        '。$ buyer.boards idArr':options.newId}
    }
);


解决方案

查询中删除的位置操作符( $ 更新参数功能。

  Meteor.users.update(
    {
        _id:options.userId,
        buyer.boards.title':options.boardTitle
    },
    {$推:{
        '。$ buyer.boards idArr':options.newId}
    }
);

从文档:

  db.collection.update(
   {<阵计算值:值...},
   {<更新运营商计算值:{<阵列GT; $:值}}

的位置经营者应在更新使用查询参数和不可以 参数。这将更新具有匹配的只有第一个对象标题

I am attempting to push data into a nested array inside of a Mongo collection. I've followed along the Mongo documentation here, http://docs.mongodb.org/manual/reference/operator/update/positional/, but have not had any luck pushing into the array. No errors or exceptions are thrown and the syntax looks right...

In this example, I'm attempting to update the buyer.boards titled 'Board One' by pushing a new string into it's idArr array. Is there something wrong with my query?

Mongo Collection

// User Document from Meteor.users Collection:
{
    _id: 'userIdqwerty',
    buyer: {
        boards: [
            {
                title: 'Board One',
                idArr: ['id123', 'id456', 'id678']
            },
            {
                title: 'Board Two',
                idArr: ['idABC']
            },
            {
                title: 'Board Three',
                idArr: ['id12345678', 'idqwertyuu']
            },
        ]
    };
}

Javascript

var options = {
    boardTitle: 'Board One',
    newId: 'idZjodFsp',
    userId: 'userIdqwerty'
};

Meteor.users.update(
    { 
        _id:options.userId, 
        'buyer.boards.$.title':options.boardTitle 
    },
    { $push: { 
        'buyer.boards.$.idArr':options.newId }
    }
);

解决方案

Remove the positional operator($) from the query parameter of the update function.

Meteor.users.update(
    { 
        _id:options.userId, 
        'buyer.boards.title':options.boardTitle 
    },
    { $push: { 
        'buyer.boards.$.idArr':options.newId }
    }
);

From the docs:

db.collection.update(
   { <array>: value ... },
   { <update operator>: { "<array>.$" : value } }
)

The positional operator should be used in the update parameter and not in the query parameter. This updates only the first boards object that has the matching title.

这篇关于MongoDB的&安培;流星 - 查询推到嵌套数组不工作,没有错误抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆