Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

mongodb - how to use aggregate function in meteor

I'm working on the following document

{
"_id" : 12,
"firstName" : "wer",
"People" : [ 
    {
        "uuid" : "123",
        "name" : "sugun",
        "person" : [ 
            {
                "uuid" : "add32",
                "name" : "ssss"
            }, 
            {
                "uuid" : "fdg456",
                "name" : "gfg"
            }
        ]
    }, 
    {
        "uuid" : "222",
        "name" : "kiran"
    }
]
} 

I want to get my output as following

{
"_id" : 456,
"People" : [ 
    {
        "uuid" : "123",
        "name" : "sugun",
        "person" : [ 
            {
                "uuid" : "add32",
                "name" : "ssss"
            }
        ]
    }
]
}

when iam using following command in mongo shell it give my required output

 db.people.aggregate([
    {$match: {_id: 12}}, 
    {$unwind: "$People"}, 
    {$unwind: "$People.person"}, 
    {$match: {"People.uuid": "123", "People.person.uuid" : "add32"}}
 ])

but when iam using same in my meteor app aggregate is not working...... so can i do the same using find or findOne methods............. or if there is any possibility to use aggregate function in my meteor app....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By using rawCollection you can pass in the same pipeline that you've been using in your mongo shell.

There is no need to install a third-party package to do this.

const stats = await MyCollection.rawCollection()
    .aggregate([
      {$match: {_id: 12}}, 
      {$unwind: "$People"}, 
      {$unwind: "$People.person"}, 
      {$match: {"People.uuid": "123", "People.person.uuid" : "add32"}}
    ])
    .toArray();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...