Pregunta

Not sure when this issue cropped up but I am not able to fetch items from mongo consistently. I have 4000+ items in the db. Here's the schema.

var Order = new Schema({
 code: {
  type: String,
  unique: true
},
...
});

Now run some queries:

Order.find().exec(function(err, orders) {
 console.log(orders.length); // always 101 
})

Order.find().limit(100000).exec(function(err, orders) {
 console.log(orders.length); // varies, sometimes 1150, 1790, 2046 - never more
})

Now if I remove the 'unique: true' from schema it will always return the total amount:

Order.find().exec(function(err, orders) {
 console.log(orders.length); // always 4213 (correct total)
})

Any idea as to why this behavior occurs? afaik the codes are all unique (orders from a merchant). This is tested on 3.8.6, 3.8.8

¿Fue útil?

Solución

Ok issue was indeed unique index being not there/corrupted. I a guilty of adding the unique index later on in the game and probably had some dups already which prevented Mongo from creating indexes.

I removed the duplicates and then in the mongo shell did this:

db.orders({name: 1}, {unique: true, dropDubs: true});

I would think the above would remove dups but it would just die because of the dups. I am sure there is a shell way to do this but I just did it with some js code then ran the above to recreate the index which can be verified with:

db.orders.getIndexes()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top