Search code examples
node.jsmongodbmongoosemongodb-querymongoose-populate

Get limited data from $unwind mongodb


I have a chats collection which given down below

{
    "_id" : ObjectId("5a44e6545818041cde24aac9"),
    "messages" : [ 
        {
            "createdAt" : ISODate("2017-12-23T07:46:29.201Z"),
            "message" : "Hii",
            "userId" : "000000"
        }, 
        {
            "createdAt" : ISODate("2017-12-23T12:46:29.201Z"),
            "message" : "Bye gtg ttyl",
            "userId" : "111111"
        }, 
        {
            "createdAt" : ISODate("2017-12-24T07:46:29.201Z"),
            "message" : "Bye Take Care",
            "userId" : "000000"
        }
    ],
    "createdAt" : ISODate("2017-10-23T07:46:29.201Z"),
    "users" : [ 
        {
            "facebookId" : "000000",
            "unread" : 0
        }, 
        {
            "facebookId" : "111111",
            "unread" : 0
        }
    ]
}

When I do this following query in nodejs

chats.aggregate( [ { $unwind : "$messages" },{ $unwind : "$users" } ])

I get an array which is something like this in which unwind opens my array something like this

[
    {
        "_id": "5a44e6545818041cde24aac9",
        "messages": {
            "createdAt": "2017-12-23T07:46:29.201Z",
            "message": "Hii",
            "userId": "000000"
        },
        "createdAt": "2017-10-23T07:46:29.201Z",
        "users": {
            "facebookId": "000000",
            "unread": 0
        }
    },
    {
        "_id": "5a44e6545818041cde24aac9",
        "messages": {
            "createdAt": "2017-12-23T07:46:29.201Z",
            "message": "Hii",
            "userId": "000000"
        },
        "createdAt": "2017-10-23T07:46:29.201Z",
        "users": {
            "facebookId": "111111",
            "unread": 0
        },
    }, //And so on.
] 

But can I only get the objects after unwind where messages.userId !== users.facebookId

something like the following object directly from MongoDB when i fetch data

[
    {
        "_id": "5a44e6545818041cde24aac9",
        "messages": {
            "createdAt": "2017-12-23T07:46:29.201Z",
            "message": "Hii",
            "userId": "000000"
        },
        "createdAt": "2017-10-23T07:46:29.201Z",
        "users": {
            "facebookId": "111111",
            "unread": 0
        },
    },{
        "_id": "5a44e6545818041cde24aac9",
        "messages": {
            "createdAt": "2017-12-23T12:46:29.201Z",
            "message": "Bye gtg ttyl",
            "userId": "111111"
        },
        "createdAt": "2017-10-23T07:46:29.201Z",
        "users": {
            "facebookId": "000000",
            "unread": 0
        }
    }, {
        "_id": "5a44e6545818041cde24aac9",
        "messages": {
            "createdAt": "2017-12-24T07:46:29.201Z",
            "message": "Bye Take Care",
            "userId": "000000"
        },
        "createdAt": "2017-10-23T07:46:29.201Z",
        "users": {
            "facebookId": "111111",
            "unread": 0
        }
    }
] 

Solution

  • Try Below one

    db.coll3.aggregate([{
                $unwind: '$users'
            }, {
                $project: {
                    messages: 1,
                    users: 1,
                    'messages': {
                        $filter: {
                            input: '$messages',
                            as: 'item',
                            cond: {
                                $ne: ['$$item.userId', '$users.facebookId']
                            }
                        }
                    }
                }
            },{ $unwind : "$messages" }
        ])
    

    This is in mongoshell. I noticed that number of zeros in your records aren't equal. Please make sure you are not confused with them even if above queries by @veeram and @Astro were giving you correct results, if you tried them