What is the use of the mongo.lock file?

Mongodb always creates the mongodb.lock file when the server starts and drops it before mongodb is stopped. Removing mongodb.lock does not affect any data it just means that mongodb was not stopped correctly. So, you are correct in removing this file and running with the -repair option should fix database.

Unique IDs with mongodb

As long as you can guarantee uniqueness, you’re not constrained to using the default “_id” MongoDB supplies. Therefore, it’s down to you how you generate this number. If you’d like to store this number inside MongoDB, then you could store it in a separate collection and increment it for every new URL required. Incrementing a … Read more

Aggregation with update in mongoDB

After a lot of trouble, experimenting mongo shell I’ve finally got a solution to my question. Psudocode: # To get the list of customer whose score is greater than 2000 cust_to_clear=db.col.aggregate( {$match:{$or:[{status:’A’},{status:’B’}]}}, {$group:{_id:’$cust_id’,total:{$sum:’$score’}}}, {$match:{total:{$gt:500}}}) # To loop through the result fetched from above code and update the clear cust_to_clear.result.forEach ( function(x) { db.col.update({cust_id:x._id},{$set:{clear:’Yes’}},{multi:true}); } ) … Read more

Mongodb: Perform a Date range query from the ObjectId in the mongo shell

You can do that in 2 steps: var objIdMin = ObjectId(Math.floor((new Date(‘1990/10/10’))/1000).toString(16) + “000 0000000000000”) var objIdMax = ObjectId(Math.floor((new Date(‘2011/10/22’))/1000).toString(16) + “000 0000000000000”) db.myCollection.find({_id:{$gt: objIdMin, $lt: objIdMax}}) or in one step (what is less readable): db.myCollection.find({_id:{$gt: ObjectId(Math.floor((new Date(‘1990/10/10’))/1000).toString(16) + “000 0000000000000”), $lt: ObjectId(Math.floor((new Date(‘2011/10/10’))/1000).toString(16) + “000 0000000000000”)}})

Sort nested array of objects

I would store it in the order you want it back out. Or sort it after you pull it out, on the client side. If neither of those are possible, you can use the aggregation framework: > db.test.insert({answers: [ … {name: ‘paul’, state: ‘RU’}, … {name: ‘steve’, state: ‘US’}, … {name: ‘mike’, state: ‘DE’}]}); > … Read more