1

New MEAN.js/Mongo developer here, I'd really appreciate any insight I can get on this issue.

Here's the relevant part of the data I'm working with, from the top of the document. I'm trying to drill down to the _id field of the objects inside the pauses array so I can match it to a given value (to access a specific instance) and perform an update on the other attributes.

{
        "_id" : ObjectId("575c902078c8db620d50699e"),
        "username" : "superadmin",
        "poms" : [
            {
                "_id" : ObjectId("577a6c6c1d4ddcf805e6a4a0"),
                "pauses" : [ 
                    {
                        "_id" : ObjectId("577a6c6e1d4ddcf805e6a4a1"),
                        "momentResumed" : null,
                        "momentPaused" : ISODate("2016-07-04T14:02:22.643Z")
                    }, 
                    {
                        "_id" : ObjectId("577a6cae1d4ddcf805e6a4a2"),
                        "momentResumed" : null,
                        "momentPaused" : ISODate("2016-07-04T14:03:26.364Z")
                    }
                ],
                "momentCompleted" : null,
                "momentStarted" : ISODate("2016-07-04T14:02:20.383Z")
            }, 
            {
                "_id" : ObjectId("577a6d63bc35cf1006c05f64"),
                "pauses" : [ 
                    {
                        "_id" : ObjectId("577a6d64bc35cf1006c05f65"),
                        "momentResumed" : null,
                        "momentPaused" : ISODate("2016-07-04T14:06:28.685Z")
                    }, 
                    {
                        "_id" : ObjectId("577a6d67bc35cf1006c05f66"),
                        "momentResumed" : null,
                        "momentPaused" : ISODate("2016-07-04T14:06:31.869Z")
                    }, 
                    {
                        "_id" : ObjectId("577a6d7ebc35cf1006c05f67"),
                        "momentResumed" : null,
                        "momentPaused" : ISODate("2016-07-04T14:06:54.594Z")
                    }
                ],
                "momentCompleted" : ISODate("2016-07-04T14:07:00.455Z"),
                "momentStarted" : ISODate("2016-07-04T14:06:27.145Z")
            }
        ]
    }

Here's the aggregation query I've come up with:

{ "$match": { "_id": req.user._id } },
  { "$unwind": "$poms" },
  { "$project": {
      "_id": 0,
      "pom": "$poms"
  } },
  { "$match": { "pom._id": req.params.pomId } },
  { "$unwind": "$pom.pauses" },
  { "$project": {
      "pause": "$pom.pauses"
  } }

Unfortunately it's giving empty brackets. I'm really stumped, I've tried everything I've been able to think of. How would you return an object with a specific _id inside from inside "pauses"?

Any help or comments would be greatly appreciated.

1 Answer 1

1

Wherever you are matching by object id. Try using this..

mongodb.ObjectID.createFromHexString(req.user._id);
mongodb.ObjectID.createFromHexString(req.params.pomId );

https://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.