MongoDB $运算符 [英] MongoDB $ operator

查看:66
本文介绍了MongoDB $运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多级架构,我正在尝试使用$运算符将元素添加到更深的列表中.

I have a multi level schema and I'm trying to add an element to a list in a deep level using the $ operator.

我的架构如下所示,我正在尝试将对象添加到特定的cList:

My schema looks like this and I'm trying to add an object to a specific cList:

var cSchema = mongoose.Schema({c:String,
    cList[
       {
         d1:String
         d2:String   
       }
    ]
});

var bSchema = mongoose.Schema({c:String,
    b:String,
    bList:[cSchema]
});


var aSchema = mongoose.Schema({
    a:String,
    aList:[bSchema]
}); 

我尝试运行以下命令,但count始终返回0:

I tried to run the following, but count always returns 0:

Model.update( {_id:req.aid, "aList._id":req.params.bid, "bList.$.cList._id":req.params.cid}, 
              {'$addToSet' : {'aList.$.bList.$.cList': req.body}}, 
              function(err,count){

              }
);

推荐答案

$运算符用于更新运算符对象(update的第二个参数),不是查询选择器(第一个参数).在查询选择器中,您应该能够使用aList.bList.cList._id,这将使aList.$.bList.$.cList对应于更新运算符中嵌入式cList的第一个匹配元素.

The $ operator is for use in the update operator object (2nd argument to update), not the query selector (1st argument). In the query selector, you should be able to use aList.bList.cList._id, and this would let aList.$.bList.$.cList correspond to the first matching element of the embedded cList in the update operator.

嵌套位置($)运算符匹配. 这张票显示了很久以前(2010年!)对此功能的需求,但是显然,mongoDB中的较低级代码无法实现这一点.看来10gen希望能在2.6版本中获得此功能.

Nested positional ($) operator matching is not supported currently. This ticket shows demand for exactly this feature from a long time ago (2010!), but apparently lower-level code within mongoDB did not make this possible. It seems 10gen is hopeful to get this feature in the 2.6 release.

看来您将不得不单独查询和更新以使用当前架构来实现此目的,或者可能将架构更改为更平坦.

It looks like you will have to separately query and update to achieve this with your current schema, or perhaps change your schema to be more flat.

这篇关于MongoDB $运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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