简体   繁体   English

mongodb“独特”的查询?

[英]mongodb “distinct” query?

I am not sure whether "distinct" is the right word for this. 我不确定“明显”是否是正确的词。

I have a simple messaging system in mongo with "sender","receiver","content". 我在mongo中有一个简单的消息系统,带有“sender”,“receiver”,“content”。

The problem I have is in the inbox page: say for example I have 3 messages from John and 4 from Jack, I would like to show in my inbox only the last message sent by each of those people. 我遇到的问题是在收件箱页面中:例如,我有来自John的3封邮件和来自Jack的4封邮件,我想在我的收件箱中仅显示每个人发送的最后一封邮件。

I have searched online and I thought this http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct would be the right thing but, it doesn't seem to work or atlest I can't get it to work... meh.. 我在网上搜索过,我认为这个http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct是正确的,但它看起来不起作用或起作用我无法得到它工作... meh ..

Ciao Enrico, Ciao Enrico,

I didn't really get your question, so I'll try to explain how distinct works: 我没有真正得到你的问题,所以我会试着解释一下有多么独特的作用:

Collection name: foo

Doc no.1: {'a':1,...}
Doc no.2: {'a':2,...}
Doc no.3: {'a':3,...}

If then you run: 如果那时你运行:

db.runCommand( { distinct: 'foo', key: 'a' } )

What you'll get will be: 你会得到什么:

[1,2,3]

And I don't think that will be of any use to you. 而且我认为这对你没有任何用处。 Instead try sorting by date sent and limiting the results to one: 而是尝试按发送日期排序并将结果限制为一个:

db.collection.find(<your_filters_here>).sort({<date_sent_key> : -1}).limit(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM