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

Database search using find() with 2 indexes of heirarchical sub-documents wrongly return empty cursor

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.2.12
    • Component/s: Querying
    • None
    • Fully Compatible
    • ALL
    • Hide

      The data:

      MongoDB Enterprise > db.coll2.find()
      { "_id" : ObjectId("58e6cf4badef354b4b538a00"), "name" : "jam", "a" : { "b" : [ 1 ], "c" : [ 11 ] } }
      { "_id" : ObjectId("58e6cf77adef354b4b538a01"), "name" : "jam", "a" : { "b" : [ 2, 3 ] } }
      { "_id" : ObjectId("58e6cf83adef354b4b538a02"), "name" : "jam", "a" : { "c" : [ 12 ] } }
      { "_id" : ObjectId("58e6cfb6adef354b4b538a03"), "name" : "jam", "a" : { "c" : [ ] } }
      { "_id" : ObjectId("58e6cfbdadef354b4b538a04"), "name" : "jam", "a" : { "b" : [ ] } }
      { "_id" : ObjectId("58e700b8adef354b4b538a05"), "name" : "jam2" }
      { "_id" : ObjectId("58e701edadef354b4b538a06"), "name" : "jam3", "a" : 34 }
      { "_id" : ObjectId("58e70244adef354b4b538a07"), "name" : "jam3", "a" : { "b" : 34 } }
      

      The indexes:

      MongoDB Enterprise > db.coll2.getIndexes()
      [
      	{
      		"v" : 1,
      		"key" : {
      			"_id" : 1
      		},
      		"name" : "_id_",
      		"ns" : "test.coll2"
      	},
      	{
      		"v" : 1,
      		"key" : {
      			"a.b.0" : 1
      		},
      		"name" : "a.b.0_1",
      		"ns" : "test.coll2"
      	},
      	{
      		"v" : 1,
      		"key" : {
      			"a.c.0" : 1
      		},
      		"name" : "a.c.0_1",
      		"ns" : "test.coll2"
      	}
      ]
      

      WRONG Result of search (looking non empty array entr(ies) in the indexed sub-documents) :

      MongoDB Enterprise > db.coll2.find({$or :[{"a.b" : {$gt : []}}, {"a.c" :{$gt : []}}] })
      MongoDB Enterprise >
      

      The result returns 0 entries.

      If remove the indexes using db.coll2.dropIndexes() and conduct the search again I get the RIGHT result as :

      MongoDB Enterprise > db.coll2.find({$or :[{"a.b" : {$gt : []}}, {"a.c" :{$gt : []}}] })
      { "_id" : ObjectId("58e6cf4badef354b4b538a00"), "name" : "jam", "a" : { "b" : [ 1 ], "c" : [ 11 ] } }
      { "_id" : ObjectId("58e6cf77adef354b4b538a01"), "name" : "jam", "a" : { "b" : [ 2, 3 ] } }
      { "_id" : ObjectId("58e6cf83adef354b4b538a02"), "name" : "jam", "a" : { "c" : [ 12 ] } }
      
      Show
      The data: MongoDB Enterprise > db.coll2.find() { "_id" : ObjectId("58e6cf4badef354b4b538a00"), "name" : "jam", "a" : { "b" : [ 1 ], "c" : [ 11 ] } } { "_id" : ObjectId("58e6cf77adef354b4b538a01"), "name" : "jam", "a" : { "b" : [ 2, 3 ] } } { "_id" : ObjectId("58e6cf83adef354b4b538a02"), "name" : "jam", "a" : { "c" : [ 12 ] } } { "_id" : ObjectId("58e6cfb6adef354b4b538a03"), "name" : "jam", "a" : { "c" : [ ] } } { "_id" : ObjectId("58e6cfbdadef354b4b538a04"), "name" : "jam", "a" : { "b" : [ ] } } { "_id" : ObjectId("58e700b8adef354b4b538a05"), "name" : "jam2" } { "_id" : ObjectId("58e701edadef354b4b538a06"), "name" : "jam3", "a" : 34 } { "_id" : ObjectId("58e70244adef354b4b538a07"), "name" : "jam3", "a" : { "b" : 34 } } The indexes: MongoDB Enterprise > db.coll2.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.coll2" }, { "v" : 1, "key" : { "a.b.0" : 1 }, "name" : "a.b.0_1", "ns" : "test.coll2" }, { "v" : 1, "key" : { "a.c.0" : 1 }, "name" : "a.c.0_1", "ns" : "test.coll2" } ] WRONG Result of search (looking non empty array entr(ies) in the indexed sub-documents) : MongoDB Enterprise > db.coll2.find({$or :[{"a.b" : {$gt : []}}, {"a.c" :{$gt : []}}] }) MongoDB Enterprise > The result returns 0 entries. If remove the indexes using db.coll2.dropIndexes() and conduct the search again I get the RIGHT result as : MongoDB Enterprise > db.coll2.find({$or :[{"a.b" : {$gt : []}}, {"a.c" :{$gt : []}}] }) { "_id" : ObjectId("58e6cf4badef354b4b538a00"), "name" : "jam", "a" : { "b" : [ 1 ], "c" : [ 11 ] } } { "_id" : ObjectId("58e6cf77adef354b4b538a01"), "name" : "jam", "a" : { "b" : [ 2, 3 ] } } { "_id" : ObjectId("58e6cf83adef354b4b538a02"), "name" : "jam", "a" : { "c" : [ 12 ] } }

      The data:

      MongoDB Enterprise > db.coll2.find()
      { "_id" : ObjectId("58e6cf4badef354b4b538a00"), "name" : "jam", "a" : { "b" : [ 1 ], "c" : [ 11 ] } }
      { "_id" : ObjectId("58e6cf77adef354b4b538a01"), "name" : "jam", "a" : { "b" : [ 2, 3 ] } }
      { "_id" : ObjectId("58e6cf83adef354b4b538a02"), "name" : "jam", "a" : { "c" : [ 12 ] } }
      { "_id" : ObjectId("58e6cfb6adef354b4b538a03"), "name" : "jam", "a" : { "c" : [ ] } }
      { "_id" : ObjectId("58e6cfbdadef354b4b538a04"), "name" : "jam", "a" : { "b" : [ ] } }
      { "_id" : ObjectId("58e700b8adef354b4b538a05"), "name" : "jam2" }
      { "_id" : ObjectId("58e701edadef354b4b538a06"), "name" : "jam3", "a" : 34 }
      { "_id" : ObjectId("58e70244adef354b4b538a07"), "name" : "jam3", "a" : { "b" : 34 } }
      

      The indexes:

      MongoDB Enterprise > db.coll2.getIndexes()
      [
      	{
      		"v" : 1,
      		"key" : {
      			"_id" : 1
      		},
      		"name" : "_id_",
      		"ns" : "test.coll2"
      	},
      	{
      		"v" : 1,
      		"key" : {
      			"a.b.0" : 1
      		},
      		"name" : "a.b.0_1",
      		"ns" : "test.coll2"
      	},
      	{
      		"v" : 1,
      		"key" : {
      			"a.c.0" : 1
      		},
      		"name" : "a.c.0_1",
      		"ns" : "test.coll2"
      	}
      ]
      

      WRONG Result of search (looking non empty array entr(ies) in the indexed sub-documents) :

      MongoDB Enterprise > db.coll2.find({$or :[{"a.b" : {$gt : []}}, {"a.c" :{$gt : []}}] })
      MongoDB Enterprise >
      

      The results returns 0 entries.

      If remove the indexes using db.coll2.dropIndexes() and conduct the search again I get the RIGHT result as :

      MongoDB Enterprise > db.coll2.find({$or :[{"a.b" : {$gt : []}}, {"a.c" :{$gt : []}}] })
      { "_id" : ObjectId("58e6cf4badef354b4b538a00"), "name" : "jam", "a" : { "b" : [ 1 ], "c" : [ 11 ] } }
      { "_id" : ObjectId("58e6cf77adef354b4b538a01"), "name" : "jam", "a" : { "b" : [ 2, 3 ] } }
      { "_id" : ObjectId("58e6cf83adef354b4b538a02"), "name" : "jam", "a" : { "c" : [ 12 ] } }
      

            Assignee:
            asya.kamsky@mongodb.com Asya Kamsky
            Reporter:
            samaresh.singh Samaresh Singh
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: