Amresh Venugopal
Amresh Venugopal

Reputation: 9549

Performing multiple operations when updating mongodb: $addToSet, $inc and aggregations: $subtract

I am learning to use mongodb and while trying to perform multiple operations in one update query, I get an error for $addToSet.

Code:

var insertBook = function(db, title, author, price, edition, img, tags, pages, user, callback) {
    db.collection('books').update( 
        { $and:[{ "title":title }, { "edition":edition }] },    
        {
            "title": title,
            "author":author, 
            {$addToSet: { "img": { $each: img }}},  //error(1)
            {$addToSet: { "tags": { $each: tags }}}, //error(2)
            "edition": edition,
            "pages":pages,
            "price":price,  
            "shared":{ $subtract: ["$shared", 0] },
            $inc: {"copies": 1},            
            "availableCopies":{ $subtract: ["$copies","$shared"] },
            {$addToSet: { "ownedBy": user }}, //error(3)
            "registeredOn": { $type: "timestamp"}
        },
        { upsert: true }

    , function(err, result) {
        assert.equal(err, null);
        console.log("Inserted a document into the Books collection.");
        callback(result);
    });
};

MongoClient.connect(url, function(err, db) {
    assert.equal(err, null);
    var title = "Harry Potter and the chamber of secrets";
    var author = "J.K. Rowling";
    var price = 50.00;
    var img = "null";
    var tags = ["Fiction", "Magic"];
    var pages = 450;
    var user = "Amresh Venugopal";
    insertBook(db, title, author, price, edition, img, tags, pages, user, function(){
        db.close();
    });
});

Error:

/home/codewingx/repo/nodeapps/RESTful/model/bookPut.js:33
        {$addToSet: { "img": { $each: img }}},
        ^

SyntaxError: Unexpected token {

Seems like there is something I have missed in the usage of $addToSet. The example at https://docs.mongodb.org/manual/reference/operator/update/addToSet/#addtoset-modifiers shows use of only the $addToSet operation.

What could be causing this error?

Upvotes: 1

Views: 368

Answers (1)

Poorna Subhash
Poorna Subhash

Reputation: 2128

Your update statement has mix of updating whole documents, specific fields and aggregate operations ($substract). You can't use aggregation operators in update staments. Without those aggregate operators, you can use update statement as below.

You also don't require $and, as the default is and operation.


  db.collection('books').update(
  { "title":title, "edition":edition },    
  {
    $set: {"title": title,
           "author": author,
           "edition": edition,
           "pages":pages,
           "price":price,
           "registeredOn": new Date()
          },
    $addToSet: { "img": { $each: img }, 
                "tags": { $each: tags }, 
                "ownedBy": user 
               },                       

    $inc: {"copies": 1}
  },
  {upsert: true},
  function (err, res) {

  });

Upvotes: 4

Related Questions