这是一份现有文件:
{
"_id": ObjectId("58bfe67c71043e8947d487c7"),
"_class": "com.plash.configurator.model.GmailMailData",
"useremail": "xyx@gmail.com",
"threadidslist": [
{
"threadid": "15aac9952c924e6c",
"subject": "dd",
"messagelist": [
{
"messageid": "15aad9c4ebba913c",
"timestamp": "Wed Mar 08 11:09:45 UTC 2017",
"from": "utsavanand.work@gmail.com",
"body": "<div dir=\"ltr\">ddd</div>\r\n",
"labelid": "RECEIVED"
}
]
}
]
}
我使用的查询是:
Update update = new Update();
update.addToSet("messagelist", gm); //gm is the object of messaglist
Criteria criteria = Criteria.where("useremail").is(useremailid)
.and("threadidslist.threadid").is(threadid);
mongoTemplate.updateFirst(Query.query(criteria), update, GmailMailData.class);
此查询在顶层插入对象,但我希望将对象添加到消息列表中。
如果 useremail 和 threadid 匹配,如何在留言列表中添加新对象?
答案 0 :(得分:1)
此查询有效:
Query query = new Query(Criteria.where("useremail").is(useremailid).and("threadidslist.threadid").is(threadid));
update.push("threadidslist.$.messagelist", gm);
mongoTemplate.updateFirst(query, update, GmailMailData.class);