任何人都可以将此转换为mongo查询。我尝试过的查询总会产生一些略有不同的错误。
正确的结果:
db.mailbox.find
( { "user_id" : "2"},
{_id: 0, mails:
{$elemMatch: {conversation_id: "3"}}}
);
以下是我尝试的内容:
Query find = new Query(
Criteria.where("user_id").is("2"))
.addCriteria(Criteria.where("_id").is(0))
.addCriteria(Criteria.where("mails")
.elemMatch(Criteria.where("conversation_id").is("3") ));
结果:
db.mailbox.find({ "user_id" : "2" , "_id" : 0 , "mails" :
{ "$elemMatch" : { "conversation_id" : "3"}}});
我也试过这个来增加未读字段:
db.mailbox.update({"user_id": "2", mails:
{$elemMatch: {conversation_id: "3"}} ,
{$inc:{"mails.$.unread":1}})
mongoTemplate.findAndModify(
Query.query(Criteria.where("user_id").is("2").and("mails")
.elemMatch(Criteria.where("conversation_id").is("3"))), new Update()
.inc("mails.$.unread", 1), MailBox.class);
我最终得到了这个错误:
{ "user_id" : "2" , "mails" : { "$elemMatch" :
{ "conversation_id" : "3"}}}
fields: null sort: null for class: class MailBox and update:
{ "$inc" : { "mails.$.unread" : 1}} in collection: mailbox