Learner
Learner

Reputation: 3

how to use findAndModify() function in mongo

with this data in collection

var documents = [{
      _id : 1
    , title: "Buy milk"
    , priority: 5
    , started: false
    , done: false
    , startTime: null
    , endTime: null
    }, {
      _id : 2
    , title: "Become a Billionaire"
    , priority: 1
    , started: false
    , done: false
    , startTime: null
    , endTime: null
    }, {
      _id : 3
    , title: "Play DOOM"
    , priority: 1000
    , started: false
    , done: false
    , startTime: null
    , endTime: null
    }
  ]

i m using findAndModify() function with following parameters:

db.test.findAndModify({started:false},{priority:-1},{$set:{started:true}},{new:true})

i m getting this error again n again


findAndModifyFailed failed: { "ok" : 0, "errmsg" : "need remove or update" } at src/mongo/shell/collection.js:399


can anybody tell me where i m making the mistake, thanks

Upvotes: 0

Views: 2895

Answers (2)

bsd
bsd

Reputation: 2717

Well, you can either update or remove a document atomically with findAndModify

db.test.findAndModify({
    query:{started:false, priority:1},
    update:{$set : {started:true}},
    new:true
})

Upvotes: 4

benjaminjosephw
benjaminjosephw

Reputation: 4417

findAndModify is used for a single document. update can be used for a number of documents when multi is set to true.

Upvotes: 0

Related Questions