如何在mongodb中使用两个条件进行更新

时间:2019-04-13 06:19:50

标签: mongodb

我正在练习mongodb。

db.inventory.insertMany( [
   { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
   { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
   { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );

我倾向于将“ cm”和“ in”分开,并进行更新,但这是行不通的

db.inventory.updateMany(
    { $or [ { "size.h": { $gt:8.75 }, "size.uom": "in" }, 
            {"size.h": { $gt:22.225 }, "size.uom": "cm"} ] },
    {
     $set: {status: "B"},
     $currentDate: {lastModified: true}
    }
)

2019-04-12T22:16:00.575-0700 E QUERY    [js] SyntaxError: missing : after property id @(shell):2:6

1 个答案:

答案 0 :(得分:0)

您的操作方式是正确的,但是存在语法错误,$or之后缺少冒号:

db.inventory.updateMany(
    { $or: [ { "size.h": { $gt:8.75 }, "size.uom": "in" }, 
            {"size.h": { $gt:22.225 }, "size.uom": "cm"} ] },
    {
    $set: {status: "B"},
    $currentDate: {lastModified: true}
    }
)