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 ?

3 Answers3

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" }}})
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • Thanks a lot ! This is what I looking for :) – Monday1994 Dec 03 '16 at 11:38
  • Does it reduce query/search time? Just curious! – Shihab Jan 19 '18 at 21:58
  • @BigGuy compare to what ? its faster as both find and projection can use index when you one. – s7vr Jan 19 '18 at 22:00
  • @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? – Shihab Jan 19 '18 at 22:02
  • @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. – s7vr Jan 19 '18 at 22:05
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.

Community
  • 1
  • 1
foxinatardis
  • 156
  • 8
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

Oladayo Oyelade
  • 225
  • 2
  • 9