我必须忽略这么简单,这是我的数据库样本:
{ "_id" : ObjectId("545e7e45a69b6c5c0caeafcc"), "url" : "http://example.com/id=4314936", "name" : "product1", "retailer" : "123", "extra" : [ ], "timestamp" : 1415478797263, "processed" : 0 }
{ "_id" : ObjectId("545e7e45a69b6c5c0caeafcc"), "url" : "http://example.com/id=4314936", "name" : "product2", "retailer" : "123", "extra" : [ ], "timestamp" : 1415478797263, "processed" : 0 }
{ "_id" : ObjectId("545e7e45a69b6c5c0caeafcc"), "url" : "http://example.com/id=4314936", "name" : "product3", "retailer" : "123", "extra" : [ ], "timestamp" : 1415478797263, "processed" : 0 }
{ "_id" : ObjectId("545e7e45a69b6c5c0caeafcc"), "url" : "http://example.com/id=4314936", "name" : "product3", "retailer" : "123", "extra" : [ ], "timestamp" : 1415478797263, "processed" : 0 }
我试图仅使用以下方法获得2个结果:
db.products.find({timestamp:1415478797263,processed:0},{},{limit:2})
我已经在Stack Overflow上阅读并尝试了类似的问题,因此请在标记为重复之前记住这一点。
无论我尝试什么,它都给了我全套...最奇怪的是我在其他地方使用类似的find()并且它有效。有经验的一双眼睛应该能够发现问题,提前谢谢!!
答案 0 :(得分:1)
使用此查询,是否有效?
db.products.find({timestamp:1415478797263,processed:0}).limit(2)
现在将它用于节点js本机驱动程序,你可以这样做
db.products.find({timestamp:1415478797263,processed:0}).limit(2).each(function(err, doc) {
console.dir(doc);
});
或以数组形式,您可以将其作为
db.products.find({timestamp:1415478797263,processed:0}).limit(2).toArray(function(err, docs) {
console.log("Returned #" + docs.length + " documents");
})