[SERVER-27679] Find query isn't picking up nested document in array Created: 13/Jan/17  Updated: 13/Jan/17  Resolved: 13/Jan/17

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Robert Young Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

I have this single document in a collection:

{
	"_id" : ObjectId("5877e601fe632e1905fda30f"),
	"created_at" : ISODate("2017-01-12T20:24:34.118Z"),
	"updated_at" : ISODate("2017-01-12T20:24:34.118Z"),
	"name" : "Creamfields 2017",
	"description" : "Creamfields Festival returns",
	"start_time" : ISODate("2017-08-24T12:00:00Z"),
	"end_time" : ISODate("2017-08-25T22:00:00Z"),
	"image_url" : "https://d31fr2pwly4c4s.cloudfront.net/c/9/f/901322_0_creamfields-2017_400.jpg",
	"buy_link" : "http://www.skiddle.com/whats-on/Warrington/Creamfields/Creamfields-2017/12846418/",
	"place" : {
		"name" : "Creamfields",
		"emails" : [ ],
		"location" : {
			"city" : "Warrington Cheshire",
			"country" : "",
			"latitude" : 53.3900441,
			"longitude" : -2.5969501,
			"street" : "daresbury",
			"zip" : "WA4 4ar"
		},
		"category_list" : [ ]
	},
	"set_times" : [
		{
			"_id" : ObjectId("5877e603fe632e1905fda311"),
			"votes" : {
				"down" : [ ],
				"up" : [
					"auth0|57927335386cd38f6f7cd9a9"
				]
			},
			"created_by" : {
				"user_id" : "a",
				"username" : "test123"
			},
			"rooms" : [
				{
					"name" : "Main",
					"start_time" : ISODate("2017-08-24T12:00:00Z"),
					"_id" : ObjectId("5877e603fe632e1905fda312"),
					"artists" : [
						{
							"external_api_id" : "19SmlbABtI4bXz864MLqOS",
							"image_url" : "https://i.scdn.co/image/10f32785a962f9aff1d217ad63aaada9394dd1e5",
							"name" : "Carl Cox",
							"end_time" : ISODate("2017-08-24T13:00:00Z"),
							"start_time" : ISODate("2017-08-24T12:00:00Z"),
							"_id" : ObjectId("5877e603fe632e1905fda313"),
							"users_to_notify" : [ ],
							"external_urls" : {
								"spotify" : "https://open.spotify.com/artist/19SmlbABtI4bXz864MLqOS"
							}
						}
					]
				}
			],
			"test" : "abc"
		},
		{
			"_id" : ObjectId("5877e612fe632e1905fda314"),
			"votes" : {
				"down" : [ ],
				"up" : [
					"auth0|57927335386cd38f6f7cd9a9"
				]
			},
			"created_by" : {
				"user_id" : "a",
				"username" : "test123"
			},
			"rooms" : [
				{
					"name" : "Main ",
					"start_time" : ISODate("2017-08-24T12:00:00Z"),
					"_id" : ObjectId("5877e612fe632e1905fda315"),
					"artists" : [
						{
							"external_api_id" : "1tRBmMtER4fGrzrt8O9VpS",
							"image_url" : "https://i.scdn.co/image/7410105fbe41f6e262da552e61e0fe456477a47c",
							"name" : "Hot Since 82",
							"end_time" : ISODate("2017-08-24T13:00:00Z"),
							"start_time" : ISODate("2017-08-24T12:00:00Z"),
							"_id" : ObjectId("5877e612fe632e1905fda316"),
							"users_to_notify" : [ ],
							"external_urls" : {
								"spotify" : "https://open.spotify.com/artist/1tRBmMtER4fGrzrt8O9VpS"
							}
						}
					]
				}
			],
			"test" : {
				"created_by" : {
					"user_id" : "a"
				}
			}
		}
	],
	"created_by" : {
		"sub" : "auth0|57927335386cd38f6f7cd9a9",
		"username" : "test123"
	},
	"__v" : 0
}

Running this command produces the wrong result:

Command:

db.events.find({ "_id":ObjectId("5877e601fe632e1905fda30f"), "set_times._id": ObjectId("5877e612fe632e1905fda314"),"set_times.created_by.user_id": "a" }, { "set_times.$": 1  }).pretty()

{
	"_id" : ObjectId("5877e601fe632e1905fda30f"),
	"set_times" : [
		{
			"_id" : ObjectId("5877e603fe632e1905fda311"),
			"votes" : {
				"down" : [ ],
				"up" : [
					"auth0|57927335386cd38f6f7cd9a9"
				]
			},
			"created_by" : {
				"user_id" : "a",
				"username" : "test123"
			},
			"rooms" : [
				{
					"name" : "Main",
					"start_time" : ISODate("2017-08-24T12:00:00Z"),
					"_id" : ObjectId("5877e603fe632e1905fda312"),
					"artists" : [
						{
							"external_api_id" : "19SmlbABtI4bXz864MLqOS",
							"image_url" : "https://i.scdn.co/image/10f32785a962f9aff1d217ad63aaada9394dd1e5",
							"name" : "Carl Cox",
							"end_time" : ISODate("2017-08-24T13:00:00Z"),
							"start_time" : ISODate("2017-08-24T12:00:00Z"),
							"_id" : ObjectId("5877e603fe632e1905fda313"),
							"users_to_notify" : [ ],
							"external_urls" : {
								"spotify" : "https://open.spotify.com/artist/19SmlbABtI4bXz864MLqOS"
							}
						}
					]
				}
			],
			"test" : "abc"
		}
	]
}

Note the "set_times._id" values do not match. I think it has something to do with the "set_times.created_by.user_id" as if I remove this, it produces the correct document.

If I update the document:

db.events.update({}, { $set: { "set_times.0.created_by.user_id": "b"  }  })

And run the script again, it produces the correct result as the "set_times.created_by.user_id" values are different.

I am using MongoDb version 3.2



 Comments   
Comment by Robert Young [ 13/Jan/17 ]

Thanks for your reply Thomas. Apologies for posting in the wrong section. I assumed it was a bug as I thought the find query matched all conditions.

Regards,,
Rob

Comment by Kelsey Schubert [ 13/Jan/17 ]

Hi noxious1991,

Thanks for the clear example. This is behavior is expected, as the first subdocument matches part of the query criteria. I would reccomend using $elemMatch to ensure that the subdocument returned matches all of the query criteria.

Please see the following simplified example:

> db.foo.findOne()
{
	"_id" : ObjectId("58792d26886ff16e9151789f"),
	"a" : [
		{
			"b" : 1,
			"c" : {
				"d" : 1
			}
		},
		{
			"b" : 2,
			"c" : {
				"d" : 1
			}
		}
	]
}
> db.foo.find({ "a.b":2, "a.c.d":1}, { "a.$": 1}).pretty()
{
	"_id" : ObjectId("58792d26886ff16e9151789f"),
	"a" : [
		{
			"b" : 1,
			"c" : {
				"d" : 1
			}
		}
	]
}
> db.foo.find({a: {$elemMatch : {"b":2, "c.d":1}}}, { "a.$": 1}).pretty()
{
	"_id" : ObjectId("58792d26886ff16e9151789f"),
	"a" : [
		{
			"b" : 2,
			"c" : {
				"d" : 1
			}
		}
	]
}

Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-users group.

Kind regards,
Thomas

Generated at Thu Feb 08 04:15:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.