Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-18959

Cannot use comparaison operator to query ObjectID in embedded documents

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 3.0.2
    • Querying
    • None
    • ALL
    • Hide

      Given that collection:

      > db.test.find().pretty()
      {
      	"_id" : ObjectId("557b43ed84ee2ba0375f4fca"),
      	"user" : {
      		"_id" : ObjectId("555b63c6b9aaf6d80aecccc3")
      	}
      }
      

      $eq comparison on ObjectID works for the root document:

      > db.test.find(
          {_id: { $eq: ObjectId("557b43ed84ee2ba0375f4fca") }}
      ).count(1)
      1
      

      But not for the embedded document:

      > db.test.find(
          {user: { _id: { $eq: ObjectId("555b63c6b9aaf6d80aecccc3") }}}
      ).count()
      0
      

      Please note that using the dot notation to access the inner field, the query works:

      > db.test.find(
          {"user._id": { $eq: ObjectId("555b63c6b9aaf6d80aecccc3") }}
      ).count()
      1
      

      Show
      Given that collection: > db.test.find().pretty() { "_id" : ObjectId("557b43ed84ee2ba0375f4fca"), "user" : { "_id" : ObjectId("555b63c6b9aaf6d80aecccc3") } } $eq comparison on ObjectID works for the root document: > db.test.find( {_id: { $eq: ObjectId("557b43ed84ee2ba0375f4fca") }} ).count(1) 1 But not for the embedded document: > db.test.find( {user: { _id: { $eq: ObjectId("555b63c6b9aaf6d80aecccc3") }}} ).count() 0 Please note that using the dot notation to access the inner field, the query works: > db.test.find( {"user._id": { $eq: ObjectId("555b63c6b9aaf6d80aecccc3") }} ).count() 1

    Description

      You cannot use the relational operators $eq or $in to query ObjectID in an embedded document.

      All is like is, for embedded documents, comparison was performed using ==, whereas for root document, it uses equals.

      Attachments

        Activity

          People

            Unassigned Unassigned
            Sylvain Leroux Sylvain Leroux [X]
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: