MongoDB $运算符

编程入门 行业动态 更新时间:2024-10-23 03:23:00
本文介绍了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 $运算符

本文发布于:2023-11-22 13:19:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!