今天碰到一個問題,當(dāng)我在update數(shù)據(jù)的時候想使用upsert功能,發(fā)現(xiàn)插入的數(shù)據(jù)并沒有過濾條件中$or的部分,如下:
db.msg.update(
{
“type”: “u”,
“$or”: [
{“from”: 1, “to”: 2},
{“from”: 2, “to”: 1}
]
},
{
“$push”: {
“message”: {
“data”: “holla”,
“status”: false,
“time”: new Date()
}
}
},
true,
true
);
本來希望的是:如果發(fā)現(xiàn)msg集合中如果沒有對應(yīng)文檔,則插入,例如上面的這條更新(假設(shè)當(dāng)前集合中不存在from:1,to:2的這個文檔),執(zhí)行完后msg集合中應(yīng)當(dāng)出現(xiàn)下面這個文檔:
{
"from": 1,
"to": 2,
"type": "u",
"message": [
{
“data”: “holla”,
“status”: false,
“time”: ISODate("2014-02-12T01:50:40.866Z")
}
]
}
可實際情況是插入的數(shù)據(jù)中并不會包含$or部分的條件,請問有沒有解決方案?
學(xué)習(xí)是最好的投資!
$upsert currently (2.4) does not support $and and $or, but it is said that 2.6 supports it. See official issues:
upsert
This method is inherently unreliable, and it is clearly written in the manual
Warning: To avoid inserting the same document more than once, only use upsert: true if the query field is uniquely indexed.
So you ignored me?