【发布时间】:2017-06-13 20:30:56
【问题描述】:
我遇到了 Cosmos DB 的问题,其中使用 {upsert: true} 和 $setOnInsert 的查询行为每次都会应用 insert 值,无论操作是否为插入或更新。
针对 Cosmos DB 和 MongoDB 运行以下示例查询的结果显示,defaultQty 的最终值存在差异。
db.products.remove({})
// WriteResult({ "nRemoved" : 1 })
db.products.insert({ _id: 1, item: "apple", price: 0.05, defaultQty: 50})
// WriteResult({ "nInserted" : 1 })
db.products.find({})
// { "_id" : 1, "item" : "apple", "price" : 0.05, "defaultQty" : 50 }
sleep(100)
db.products.update(
{ _id: 1 },
{ $set: { price: 0.10 }, $setOnInsert: { defaultQty: 100 }},
{ upsert: true }
)
// WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.products.find({})
// { "_id" : 1, "item" : "apple", "price" : 0.1, "defaultQty" : 100 }
这是comparison results 在 Studio 3T 中并排的屏幕截图。
有人经历过吗?
谢谢!
【问题讨论】:
标签: mongodb azure azure-cosmosdb