简体   繁体   中英

Complex MongoDB collection query with NodeJS

I have large collection of documents in MongoDB with the following structure as an example:

{ "key" : "a" ,"data" : "value1" , "lastname" : "Doe" }<br>
{ "key" : "ab" , "data" : "value1" , "lastname" : "Doe" }<br>
{ "key" : "abc" ,   "data" : "value1" , "lastname" : "Doe" } <<< <br>
{ "key" : "d" ,     "data" : "value2" , "lastname" : "Doe" }<br>
{ "key" : "df" ,    "data" : "value2" , "lastname" : "Doe" }<br>
{ "key" : "dfg" ,   "data" : "value2" , "lastname" : "Doe" } <<< <br>

I need to get data from those two lines marked by <<< based on terms that key on those lines already includes key values from other documents for the same lastname ie "abc" has "ab" and "ab" has "a", result I needed should look like this:

{ "lastname" : "Doe" , "value1" : "abc" , "value2" : "dfg" }

I can pull data in 2 passes by creating 2 separate queries , but I want to see if this can be done in just one.

Not yet fully formatted :

db.coll.aggregate([
 {$project: {key:1, root: '$$ROOT', lastname: 1, data: 1, keyLength: {$strLenBytes: '$key'}, keyIndexes: {$range:[0,  {$strLenBytes: '$key'}]}}}, 
 {$unwind: '$keyIndexes'}, 
 {$group: {items: {$push: '$root'}, count: {$sum: 1}, _id: {$substrBytes: ['$key', '$keyIndexes', 1]}}}, 
 {$match: {count: 1}}, 
 {$group: {_id: '$items.lastname', items: {$push: {$arrayElemAt: ['$items', 0]}}}}
]).pretty()
{
    "_id" : [
        "Doe"
    ],
    "items" : [
        {
            "_id" : ObjectId("58f62e5433530ab32f554fd6"),
            "key" : "abc",
            "data" : "value1",
            "lastname" : "Doe"
        },
        {
            "_id" : ObjectId("58f62e5433530ab32f554fd9"),
            "key" : "dfg",
            "data" : "value2",
            "lastname" : "Doe"
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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