MongoDB无法获得所有的行

我有一个使用MEAN堆栈的项目,在Mongo DB集合中,我有一个名为'services'的集合,包含以下行(一个小子集):

{ "_id" : ObjectId("55af611de2be6d817d000001"), "client" : "55a8e5fa586f94752a000002", "collaborators" : null, "date" : ISODate("2015-07-22T09:23:41.917Z"), "latestUpdateDate" : ISODate("2015-07-22T09:23:41.917Z"), "backofficeRequested" : false, "description" : "Uma descrição qq", "wasSeen" : 0, "currentStatusId" : "0", "lastStatusId" : 4, "__v" : 2, "addresses" : [ ], "dates" : [ ], "estimateDuration" : 61, "estimatePrice" : "55", "notes" : [ ], "serviceDate" : ISODate("2015-07-23T10:07:00Z"), "totalPrice" : "600", "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" } { "_id" : ObjectId("55afa8662c1e98c80d2fd2ff"), "client" : "55a8e5fa586f94752a000002", "estimatePrice" : "12", "estimateDuration" : NumberLong(720), "description" : "12", "serviceDate" : ISODate("1212-12-12T12:48:45Z"), "lastStatusId" : null, "currentStatusId" : "0", "collaborators" : [ { "id" : "55acb6022c1e98d05f2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" } { "_id" : ObjectId("55afa9f62c1e98370e2fd2ff"), "client" : "55ae68d82c1e98ac782fd303", "estimatePrice" : "12", "estimateDuration" : NumberLong(720), "description" : "12", "serviceDate" : ISODate("1212-12-12T12:48:45Z"), "lastStatusId" : null, "currentStatusId" : "1", "collaborators" : [ { "id" : "55acb6022c1e98d05f2fd2ff" } ], "dates" : [ ], "totalPrice" : "500", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" } { "_id" : ObjectId("55afaa7e2c1e98bc0d2fd2ff"), "client" : "55a8e5fa586f94752a000002", "estimatePrice" : "12", "estimateDuration" : NumberLong(720), "description" : "12", "serviceDate" : ISODate("1212-12-12T12:48:45Z"), "lastStatusId" : null, "currentStatusId" : "0", "collaborators" : [ { "id" : "55acb6022c1e98d05f2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" } { "_id" : ObjectId("55afaf582c1e98d80d2fd303"), "client" : "55ae68d82c1e98ac782fd303", "estimatePrice" : "1", "estimateDuration" : NumberLong(60), "description" : "1", "serviceDate" : ISODate("1111-11-11T11:47:45Z"), "lastStatusId" : null, "currentStatusId" : "4", "collaborators" : [ { "id" : "55ae40772c1e98007a2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" } { "_id" : ObjectId("55afb8472c1e98370e2fd300"), "client" : "55a8e5fa586f94752a000002", "estimatePrice" : "1", "estimateDuration" : NumberLong(60), "description" : "1", "serviceDate" : ISODate("1111-11-11T11:47:45Z"), "lastStatusId" : null, "currentStatusId" : NumberLong(2), "collaborators" : [ { "id" : "55ae40772c1e98007a2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" } { "_id" : ObjectId("55aff2dc2c1e98ac112fd2ff"), "client" : "55a8e5fa586f94752a000002", "estimatePrice" : "1", "estimateDuration" : NumberLong(720), "description" : "1", "serviceDate" : ISODate("1989-12-12T12:12:00Z"), "lastStatusId" : null, "currentStatusId" : NumberLong(2), "collaborators" : [ { "id" : "55ae40772c1e98007a2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" } 

我想获取属于客户端的所有行ID“55a8e5fa586f94752a000002”。

在mongo Shell中执行起来非常简单:

 db.services.find({"client":"55a8e5fa586f94752a000002"}) 

但是我在Node.js里面很难,因为它根本没有返回任何服务,或者只返回一个服务。

这是我现在使用的:

 (...) console.log(checkDatas[0]._id); Service.find().where('client', checkDatas[0]._id) .exec(function(err, services) { console.log("output: " + services); (...) 

最后,这是输出:

 **55a8e5fa586f94752a000002** **output**: { _id: 55aff719af19b58913000001, client: 55a8e5fa586f94752a000002, __v: 1, collaborators: [ { id: '55acb6022c1e98d05f2fd2ff' } ], notes: [], dates: [], addresses: [], date: Wed Jul 22 2015 22:03:37 GMT+0200 (CEST), latestUpdateDate: Wed Jul 22 2015 22:03:37 GMT+0200 (CEST), backofficeRequested: false, totalPrice: null, estimatePrice: null, estimateDuration: null, description: '', serviceDate: null, wasSeen: 0, currentStatusId: 3, lastStatusId: 1 } 

就好像我正在使用findOne,但我不是。

最重要的是,昨天我在这个查询中也有一个.sort()方法,事情在评论之后仍然正常,但是现在问题已经回来了。

MongoDB / Node是否有某种我不知道的caching系统?

谢谢

编辑

运行

 Service.find({'client': checkDatas[0]._id}) 

产生相同的结果

它看起来像混合ObjectId和string,这是两个不同的东西。

您的Mongoose查询正在寻找一个ObjectId

 Service.find().where('client', checkDatas[0]._id) 

快速提示:您可以在输出中区分这两种types:

 // ObjectId, because it doesn't have quotes around it client: 55a8e5fa586f94752a000002 // String collaborators: [ { id: '55acb6022c1e98d05f2fd2ff' } ] 

你的MongoDB shell查询正在寻找一个string:

 db.services.find({"client":"55a8e5fa586f94752a000002"}) 

你发布的子集只显示string,但我认为你的数据库可能实际上包含这两种types。 你可以检查一下,看看这个效果是否更好:

 Service.find().or([ { client : checkDatas[0]._id }, { client : String(checkDatas[0]._id) } ])... 

显然这并不理想,如果两种types混合使用,都应该考虑规范化数据库。

编辑or使用Mongoose时,查询可能无法正常工作,因为它会将这两个子句转换为架构中定义的types。 你仍然可以从Mongo shell中检查:

 db.services.find({ $or : [ { client : "55a8e5fa586f94752a000002" }, { client : ObjectId("55a8e5fa586f94752a000002") } ]})