Liberateur
Liberateur

Reputation: 1487

Mongodb find not find without _id

The searches work strangely with my database in production: if I do not specify the _id, it does not find the object.

When I search with the _id only, it works:

MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:hidevalue000/hidevalue001
MongoDB server version: 3.6.2
> hidevalue001:PRIMARY> 
> hidevalue001:PRIMARY> 
> hidevalue001:PRIMARY> 
> hidevalue001:PRIMARY> db.trackings.find({_id:ObjectId('hidevalue002')}).pretty()
{
    "_id" : ObjectId("hidevalue002"),
    "public" : false,
    "creation_date" : ISODate("2018-03-08T09:43:27.661Z"),
    "user_id" : ObjectId("hidevalue003"),
    "apikey" : "hidevalue004",
    "type" : "autodiag",
    "key" : "autodiag_data",
    "value" : "hidevalue005",
    "__v" : 0
}
> hidevalue001:PRIMARY> 
> hidevalue001:PRIMARY> 

When I do a search without the _id, it does not return anything to me:

> hidevalue001:PRIMARY> db.trackings.find(
... {
...     "user_id" : ObjectId("hidevalue003"),
...     "apikey" : "hidevalue004",
...     "type": "autodiag",
...     "key" : "autodiag_data"
... }).pretty()
> hidevalue001:PRIMARY> 
> hidevalue001:PRIMARY> 

When I search with the _id, it returns me well the result:

> hidevalue001:PRIMARY> db.trackings.find(
... {
...     "_id" : ObjectId("hidevalue002"),
...     "user_id" : ObjectId("hidevalue003"),
...     "apikey" : "hidevalue004",
...     "type": "autodiag",
...     "key" : "autodiag_data",
... }).pretty()
{
    "_id" : ObjectId("hidevalue002"),
    "public" : false,
    "creation_date" : ISODate("2018-03-08T09:43:27.661Z"),
    "user_id" : ObjectId("hidevalue003"),
    "apikey" : "hidevalue004",
    "type" : "autodiag",
    "key" : "autodiag_data",
    "value" : "hidevalue005",
    "__v" : 0
}
> hidevalue001:PRIMARY> 
> hidevalue001:PRIMARY> 

The total of trackings in base:

> hidevalue001:PRIMARY> db.trackings.find().count()
38330

The MongoDB index is configured by Mongoose version 5.0.1 (NodeJS):

// Init
var trackingsSchema = new mongoose.Schema(
{
    user_id:       { type:mongoose.Schema.Types.ObjectId },
    apikey:        { type:String },
    type:          { type:String },
    key:           { type:String },
    value:         { type:String },
    public:        { type:Boolean, default:false },
    creation_date: { type:Date, default:Date.now }
});

// Set index
trackingsSchema.index(
{
    'user_id': 1,
    'apikey':  1,
    'type':    1,
    'key':     1,
    'value':   1,
    'public':  1,
    'creation_date':-1
}, { name: 'hidevalue006' });

EDIT :

If I delete user_id in the search, then I find my document.

> hidevalue001:PRIMARY> db.trackings.find(
... {
...     "apikey" : "hidevalue004",
...     "type": "autodiag",
...     "key" : "autodiag_data",
...     "creation_date" : ISODate("2018-03-08T09:43:27.661Z")
... }).pretty()
{
    "_id" : ObjectId("hidevalue002"),
    "public" : false,
    "creation_date" : ISODate("2018-03-08T09:43:27.661Z"),
    "user_id" : ObjectId("hidevalue003"),
    "apikey" : "hidevalue004",
    "type" : "autodiag",
    "key" : "autodiag_data",
    "value" : "hidevalue005",
    "__v" : 0
}
> hidevalue001:PRIMARY> 
> hidevalue001:PRIMARY> 

The db.trackings.validate () command does not return an error.

hidevalue001:PRIMARY> db.trackings.validate()
{
    "ns" : "hidevalue000.hidevalue001",
    "nInvalidDocuments" : NumberLong(0),
    "nrecords" : 39024,
    "nIndexes" : 2,
    "keysPerIndex" : {
        "hidevalue002.trackings.$_id_" : 39024,
        "hidevalue002.trackings.$hidevalue003" : 38494
    },
    "valid" : true,
    "warnings" : [
        "Some checks omitted for speed. use {full:true} option to do more thorough scan."
    ],
    "errors" : [ ],
    "ok" : 1
}

EDIT :

I have try db.trackings.reIndex(), same result.


EDIT :

I create a new collection and reindex all my documents, and same result, I can not find my document if I specify the user_id.

> db.createCollection('trackings_v2');
> 
> 
> db.trackings_v2.createIndex(
{
    'user_id':       1
    'apikey':        1
    'type':          1
    'key':           1
    'value':         1
    'ip':            1
    'public':        1
    'creation_date': 1
},
{ name: 'hidevalue010' });
> 
> 
> db.trackings.find({}).forEach(function(e)
{
    delete e._id;
    delete e.__v;
    db.trackings_v2.update(e, e, {upsert:true});
})
> 
> 

EDIT :

some trackings work with the same user_id, so some trakings are probably badly indexed ...


SOLUTION :

The value of the value field was greater than 1024 bytes, mongoDB does not like big values if we do not specify it ... I did not think he would not put any log on it ...

Change trackings index :

trackingsSchema.index(
{
    'user_id': 1,
    'apikey':  1,
    'type':    1,
    'key':     1,
    'value':   'text',
    'public':  1,
    'creation_date':-1
}, { name: 'hidevalue006' });

ReIndex all collections (or just trackings) :

db.getCollectionNames().forEach(function(collname)
{
  db.getCollection(collname).reIndex();
});

Upvotes: 1

Views: 1144

Answers (1)

Liberateur
Liberateur

Reputation: 1487

I found solution...

The value of the value field was greater than 1024 bytes, mongoDB does not like big values if we do not specify it ... I did not think he would not put any log on it ...

Change trackings index :

trackingsSchema.index(
{
    'user_id': 1,
    'apikey':  1,
    'type':    1,
    'key':     1,
    'value':   'text',
    'public':  1,
    'creation_date':-1
}, { name: 'hidevalue006' });

ReIndex all collections (or just trackings) :

db.getCollectionNames().forEach(function(collname)
{
  db.getCollection(collname).reIndex();
});

Upvotes: 1

Related Questions