不一致的mongo结果具有独特的领域

时间:2014-04-25 02:45:17

标签: javascript node.js mongodb mongoose

不确定此问题何时出现,但我无法始终从mongo中获取项目。我在db中有4000多个项目。这是架构。

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

现在运行一些查询:

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
})

现在,如果我删除了' unique:true'从架构中,它将始终返回总金额:

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

知道为什么会出现这种情况? afaik代码都是唯一的(来自商家的订单)。这是在3.8.6,3.8.8

上测试的

1 个答案:

答案 0 :(得分:0)

好的问题确实是唯一的索引没有/已损坏。我后来在游戏中添加了唯一索引,并且可能已经有一些重复,这使得Mongo无法创建索引。

我删除了重复项,然后在mongo shell中执行了此操作:

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

我认为上面会删除重复,但它会因为重复而死亡。我确信有一种shell方法可以做到这一点,但我只是使用一些js代码然后运行上面重新创建索引,可以通过以下方式验证:

db.orders.getIndexes()
相关问题