简体   繁体   English

嵌套结构mongodb下如何使用$elemMatch

[英]How to use $elemMatch under nested structure mongodb

I have a dataset Like this:我有一个这样的数据集:

{
    "_id" : ObjectId("5ede21062ce68d29740a861a"),
    "a" : {
        "b" : 0,
        "c" : [ 
            {
                "e" : "5c35f1045643180d9488112f",
                "f" : 1,
                "g" : 2
            }
        ],
        "d" : []
    },
},
{
    "_id" : ObjectId("5ede21062ce68d29740a861b"),
    "a" : {
        "b" : 0,
        "c" : [ 
            {
                "e" : "5c35f1045643180d9488112g",
                "f" : 21,
                "g" : 22
            }
        ],
        "d" : []
    },
}

Now I want to check my 5c35f1045643180d9488112f id exist on e column or not.现在我想检查我的5c35f1045643180d9488112f id 是否存在于e列。 So if not exist then I will push the objects on c array.所以如果不存在,那么我会将对象推送到 c数组上。 If exist then I will increase the counter of g .如果存在,那么我将增加g的计数器。

I used the $elemMatch approach but I am getting the error ie * Cannot use $elemMatch projection on a nested field.*我使用了$elemMatch方法,但出现错误,即 * 无法在嵌套字段上使用 $elemMatch 投影。*

I am using Mongoose and node.js我正在使用 Mongoose 和 node.js

My Code is:我的代码是:

db.Collection.findOne(
 {_id: ObjectId('5ede21062ce68d29740a861a')},
 {'a.c': {$elemMatch: {e: '5c35f1045643180d9488112f'}}}
);
it generates the error -> Cannot use $elemMatch projection on a nested field.

then I tried other approach.然后我尝试了其他方法。 In this I am not getting the data but also not getting the result properly.在这我没有得到数据,但也没有得到正确的结果。 it returns the Blank array.它返回空白数组。

db.Collection.findOne(
 {_id: ObjectId('5ede21062ce68d29740a861a')},
 {'a': {$elemMatch: {c: {$elemMatch: {e: '5c35f1045643180d9488112f'}}}}}
);
//response id --> {_id: ObjectId('5ede21062ce68d29740a861a'), a: { b: 0, c: [], d: [] }}

Can anyone suggest me where I have done the mistake.谁能建议我在哪里做错了。 Or What is the efficient and suggestable way to achieve this.或者什么是实现这一目标的有效且可行的方法。

Simple way will be first find the data then use javascript to check value exist in array of objects.简单的方法是首先找到数据,然后使用 javascript 检查对象数组中是否存在值。 If exist then write the update query which will increment the counter, if not exist then push the object on array.如果存在,则编写将增加计数器的更新查询,如果不存在,则将 object 推送到阵列上。

But I want to accomplish this through DB Query.但我想通过 DB Query 来实现这一点。 Any hellp is really appreciated.任何问题都非常感谢。 Thanks in Advance提前致谢

Your first approach was almost correct.您的第一种方法几乎是正确的。 But there is a syntax error.但是有一个syntax错误。 No need to use separate curly brackets for _id and other conditions. _id和其他条件无需使用单独的大括号。

db.Collection.findOne(
 {_id: ObjectId('5ede21062ce68d29740a861a'),
'a.c': {$elemMatch: {e: '5c35f1045643180d9488112f'}}}
);

Query is:查询是:

db.Collection.findOne(
 {
  _id: ObjectId('5ede21062ce68d29740a861a'),
  'a.c.e': '5c35f1045643180d9488112f'
 }
);

So if Id exist then it return the data .因此,如果 Id 存在,则它返回数据 If not exist then It return the null .如果不存在,则返回null

So if null comes means value not exist so we have to push the object.因此,如果 null 出现意味着值不存在,那么我们必须推送 object。

If value exist then we have to increment the counter如果值存在,那么我们必须增加计数器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM