products.insertMany

an anonymous user · January 30, 2024
db.createCollection("products");
db.products.insertMany([
    { _id: ObjectId("65b8b63b4c2a33895bdd61f5"), name: "Hat", onSale: true }, 
    { _id: ObjectId("65b8b63b4c2a33895bdd61f6"), name: "Scarf"}, 
    { _id: ObjectId("65b8b63b4c2a33895bdd61f7"), name: "Gloves", onSale: false},
    { _id: ObjectId("65b8b63b4c2a33895bdd61f8"), name: "Shoes"}
]); 
db.products.find({});
db.products.updateMany({},
    [ 
        { 
            $set: { 
                onSale: {
                    "$cond": {
                        "if": {
                            "$in": [
                                "$_id",
                                [
                                    ObjectId("65b8b63b4c2a33895bdd61f7"), 
                                    ObjectId("65b8b63b4c2a33895bdd61f8")
                                ]
                            ]
                        },
                        "then": true,
                        "else": false
                    } 
                }
            } 
        },
    ]
);
db.products.find({});
Output

Comments

Please sign up or log in to contribute to the discussion.