任何人都可以将其转换为 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