1

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

Community
  • 1
  • 1
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
  • okay will attach screenshot in a while pls hold on – Vignesh Subramanian Aug 10 '15 at 04:36
  • plz no screenshots. It is not that hard to copy a normal text document, but it will be easy to use it. – Salvador Dali Aug 10 '15 at 04:37
  • added both screenshot and plain text – Vignesh Subramanian Aug 10 '15 at 05:02
  • possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – Blakes Seven Aug 10 '15 at 05:10
  • @BlakesSeven I have already tried using the $elemmatch as suggested in one of the answers, but still i am getting all the stories, not sure what i am missing – Vignesh Subramanian Aug 10 '15 at 06:09
  • @vignesh Then you are using different code to what you are posting, If anything I would supect your code to return "nothing" since `req.body.storyId` would be a string and you are trying to match to an `ObjectId`. The linked answer(s) contain examples or all possiblities, singular match and multiple matches. Follow them. And make sure to cast to the correct type. – Blakes Seven Aug 10 '15 at 06:13
  • yes Blake I tried using ObjectID(storyId), it returned the parent (post) with all the stories – Vignesh Subramanian Aug 10 '15 at 06:17
  • @BlakesSeven I have updated my code in question pls check – Vignesh Subramanian Aug 10 '15 at 08:36

1 Answers1

2

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()
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753