Cannot use comparaison operator to query ObjectID in embedded documents

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • None
    • Affects Version/s: 3.0.2
    • Component/s: 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
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None

      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.

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

              Created:
              Updated:
              Resolved: