gpt4 book ai didi

mongodb - Meteor 对集合有独特的查询吗?

转载 作者:行者123 更新时间:2023-12-01 23:04:52 25 4
gpt4 key购买 nike

我想返回我的集合中的不同字段。我知道这些是 docs for mongo operators ,但我对查询语言不够熟悉,不知道这是否可行?

Meteor.publish("distinctCollection", function() {
return Collection.find({ ... }, fields: { "myField": 1 });
});

最佳答案

Collection.find({}).distinct('myField', true);

要使用,请将以下内容放入[project]/client/lib/a.js:

LocalCollection.Cursor.prototype.distinct = function (key,random) {
var self = this;

if (self.db_objects === null)
self.db_objects = self._getRawObjects(true);
if (random)
self.db_objects = _.shuffle(self.db_objects);
if (self.reactive)
self._markAsReactive({ordered: true,
added: true,
removed: true,
changed: true,
moved: true});
var res = {};
_.each(self.db_objects,function(value){

if(!res[value[key]]){
res[value[key]] = value;
}
});
return _.values(res);
};

关于mongodb - Meteor 对集合有独特的查询吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com