Vignesh Subramanian
Vignesh Subramanian

Reputation: 7289

selecting array element using unique id not working in mongodb

My application has "Posts" which will have stories

Now I want to select one post and only one of its stories from multiple stories in the story array

var db = req.db;
var mongo = require('mongodb');
var collection = db.get('clnPost');
var userId = req.body.userId;
var postId = req.body.postId;
var storyId = req.body.storyId;
var ObjectID = mongo.ObjectID;
 var mongo = require('mongodb');
var ObjectID = mongo.ObjectID;
collection.find({ _id: ObjectId(postId), "Stories._id": ObjectID(req.body.storyId)}, function (e, docs) {
//some processing on the docs
}

I want this to return the post along with only story which has the story Id that is sent through the request, but its returning all the stories in the stories array

enter image description here

Here I am getting all the stories for that post, where as i need only the story with Id that matches req.body.storyId

I also tried using $elemMatch after checking this question but still got the same results

collection.find({ _id: postId}, {Stories: { $elemMatch: { _id: req.body.storyId } } }, function (e, docs) {

I also tried

collection.find({ "Stories._id":ObjectID(storyId)}, {"Stories.$":1,Stories: {$elemMatch: { "Stories._id" :ObjectID(storyId) }} } , function (e, docs) {

The structure of the post document is as follows

{
    "_id": {
        "$oid": "55a7847ee4b07e03cc6acd21"
    },

    "Stories": [
        {
            "userId": "743439369097985",
            "story": "some story goes on here",
            "_id": {
                "$oid": "55c0b9367d1619a81daaefa0"
            },
            "Views": 29,
            "Likes": []
        },
        {
            "userId": "743439369097985",
            "story": "some story goes on here",
            "_id": {
                "$oid": "55c0bbd97abf0b941aafa169"
            },
            "Views": 23,
            "Likes": []
        }
    ]
}

I also tried using $unwind

 var test= collection.aggregate( //where collection =db.get('clnPost')
            { $match: { "Stories._id": ObjectID(storyId) } },
            { $project : { Stories : 1 } },
            { $unwind : "$Stories" }
            );

Update 1: i am using monk and hence aggregate is not working

Upvotes: 1

Views: 172

Answers (1)

Salvador Dali
Salvador Dali

Reputation: 222581

I can not copy your example document, so I used similar one:

{
    "_id" : "55a7847ee4b07e03cc6acd21",
    "Stories" : [{
        "userId" : "743439369097985",
        "story" : "some story goes on here",
        "_id" : "55c0b9367d1619a81daaefa0",
        "Views" : 29,
        "Likes" : [ ]
    }, {
        "userId" : "743439369097985",
        "story" : "some story goes on here",
        "_id" : "55c0bbd97abf0b941aafa169",
        "Views" : 23,
        "Likes" : [ ]
    }]
}

To get what you want, you need to use dollar projection operator in a way I used it here.

db.collection.find({
   "Stories._id": "55c0bbd97abf0b941aafa169"
}, {
   "Stories.$": 1
}).pretty()

Upvotes: 2

Related Questions