mongodb - 如何在查询中从 Waterline 0.10.x 中提取不同的值?

标签 mongodb distinct sails.js waterline

所以我在水线 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/

相关文章:

MongoDB:将多个数值连接到字符串

有限制的 MySQL top count({column})

xml - 在 Xpath 中不同?

javascript - SailsJS 无法访问位于 asset/app 中的 index.html

javascript - 在 sails.js 下使用 SSL 时 Passport.js 无法初始化

javascript - Mongoose 3.8.31 中未声明 'distinct' 的值

C# 将两个集合连接在一起

mysql - 将包含 sum 和 group_concat 的 mysql 查询转换为 mongodb 查询

mysql - 查找 MySQL 数据库中两列组合的重复项

mysql - 如何在 sails 中使用 where 子句和 'or pairs' 子句进行查询