0

When I try to find specific object in array using find({query}) I always get all elements from array. Activities array stores activities (it would be a thousands of them) as you can see in the following snippet:

This is my collection:

{
    "_id" : ObjectId("58407140755324d04db2ce95"),
    "owner" : 103429326776572,
    "activities" : [
            {
                    "name" : "test1",
                    "startTime" : ISODate("2016-08-11T17:41:54Z"),
                    "type" : "te1",
                    "lat" : 1,
                    "lon" : 1,
                    "creator" : 126212904493088,
                    "coverPhoto" : {
                            "name" : "test1",
                            "path" : "c:\\Users\\Francis\\Desktop\\dusk\\public\\coverPhotos\\SJ9tpP6Mx.jpg"
                    },
                    "identifier" : "H1g9F6vpGl",
                    "users" : [
                            1,
                            2,
                            3
                    ],
                    "hashTags" : [
                            "some",
                            "hashtags"
                    ]
            },
            {
                    "name" : "test2",
                    "startTime" : ISODate("2016-08-11T17:41:53Z"),
                    "type" : "te2",
                    "lat" : 1,
                    "lon" : 1,
                    "creator" : 103312904493090,
                    "coverPhoto" : {
                            "name" : "test2",
                            "path" : "c:\\Users\\Francis\\Desktop\\dusk\\public\\coverPhotos\\Hy8qpvafe.jpg"
                    },
                    "identifier" : "rJlU5TvpMx",
                    "users" : [
                            1,
                            2,
                            3
                    ],
                    "hashTags" : [
                            "some",
                            "hashtags"
                    ]
            }
    ]

}

I need to get for example an activity that has specific identifier. I tried to use queries like:

1) db.myCollection.find({'activities.identifier' : "rJlU5TvpMx"})

2) db.myCollection.find({'activities' : { $elemMatch : { "identifier" : "rJlU5TvpMx", "creator" : 103312904493090 } })

And all combinations with '' or "" signs

I found above queries at mongodb docs in equal documents schema as mine is.

Can you tell me what am I doing wrong ?

1
  • what is the error message you are getting? Commented Dec 2, 2016 at 22:47

3 Answers 3

2

You can try either use single match or multiple match based on your need. This makes use of $elemMatch(projection)

db.myCollection.find({"_id" : ObjectId("58407140755324d04db2ce95")},
             {activities: {$elemMatch: { identifier: "rJlU5TvpMx"}}})

db.myCollection.find( {"_id" : ObjectId("58407140755324d04db2ce95")},
             {activities: {$elemMatch: {creator : 103312904493090, identifier: "rJlU5TvpMx" }}})
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot ! This is what I looking for :)
Does it reduce query/search time? Just curious!
@BigGuy compare to what ? its faster as both find and projection can use index when you one.
@Veeram Like my find query returns all the documents including all arrays/sub-documents. So, does $elemMatch help somehow to reduce query to find what I exactly want?
@BigGuy $elemMatch is a search operator to query embedded arrays with multiple query criteria. It can use index when available. Not sure how it is implemented behind the scene though it only looks for first matching document.
0

You are looking for the projection object which gets passed as an argument in your query. It allows the return of specific fields from your search rather than the entire document. http://mongoosejs.com/docs/api.html#model_Model.find

I would also suggest looking at the response to this question here: Mongoose Query: Find an element inside an array which makes use of the unwind operator to enter the array as it seems to be relevant to your needs.

Comments

0

In the collection you are searching in, you have just one Document(Object). If you apply method find() to your collection and the query inside matches the value in activities.identifier it will return the only Document(object).

To have a better understanding of what I am talking about check example on mongoose API doc And query result here.

Try check this out https://docs.mongodb.com/v3.0/reference/operator/projection/elemMatch/#proj._S_elemMatch instead

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.