所以我在水线 0.10.x 中寻找 .distinct 函数但未能找到,在查看 Stack Overflow 后我发现了这个......
How to extract distinct values from a mongo database using Waterline and Sails.js (version 0.10)?
这几乎解决了我的问题。
所以我尝试查看 native mongodb 文档,发现我可以做到这一点......
db.dogs.distinct('breed', { owner: ObjectId('123456') })
返回 ObjectId('123456') 拥有的不同品种。
但这不起作用...
Dog.native(function (err, collection){
collection.distinct('breed', { owner: '123456' }, function (err, breeds){
console.log(breeds);
});
});
这给了我一个空数组。
我认为问题在于 Waterline 的 .native 函数不知道何时将给定字符串转换为 ObjectId 并且 Waterline 文档仍然在 -记录在案。
如何通过查询获取不同的值?
最佳答案
我找到了解决方案。
sails-mongo v0.10.5 引入了结果中使用的函数。
可以在这里看到:https://github.com/balderdashy/sails-mongo/pull/215
工作代码如下:
Dog.native(function (err, collection){
collection.distinct('breed', {
owner: Owner.mongo.objectId("123456")
}, function (err, breeds){
console.log(breeds);
});
});
Model.mongo.objectId
函数是在 sails-mongo 0.10.5 中引入的。
关于mongodb - 如何在查询中从 Waterline 0.10.x 中提取不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28806018/