How to export all collections in MongoDB?

For lazy people, use mongodump, it’s faster: mongodump -d <database_name> -o <directory_backup> And to “restore/import” it (from directory_backup/dump/): mongorestore -d <database_name> <directory_backup> This way, you don’t need to deal with all collections individually. Just specify the database. Note that I would recommend against using mongodump/mongorestore for big data storages. It is very slow and once … Read more

How to remove a field completely from a MongoDB document?

Try this: If your collection was ‘example’ db.example.update({}, {$unset: {words:1}}, false, true); Refer this: http://www.mongodb.org/display/DOCS/Updating#Updating-%24unset UPDATE: The above link no longer covers ‘$unset’ing. Be sure to add {multi: true} if you want to remove this field from all of the documents in the collection; otherwise, it will only remove it from the first document it … Read more

Add new field to every document in a MongoDB collection

Same as the updating existing collection field, $set will add a new fields if the specified field does not exist. Check out this example: > db.foo.find() > db.foo.insert({“test”:”a”}) > db.foo.find() { “_id” : ObjectId(“4e93037bbf6f1dd3a0a9541a”), “test” : “a” } > item = db.foo.findOne() { “_id” : ObjectId(“4e93037bbf6f1dd3a0a9541a”), “test” : “a” } > db.foo.update({“_id” :ObjectId(“4e93037bbf6f1dd3a0a9541a”) },{$set : … Read more

Retrieve only the queried element in an object array in MongoDB collection

MongoDB 2.2’s new $elemMatch projection operator provides another way to alter the returned document to contain only the first matched shapes element: db.test.find( {“shapes.color”: “red”}, {_id: 0, shapes: {$elemMatch: {color: “red”}}}); Returns: {“shapes” : [{“shape”: “circle”, “color”: “red”}]} In 2.2 you can also do this using the $ projection operator, where the $ in a … Read more

How to execute mongo commands through shell scripts?

You can also evaluate a command using the –eval flag, if it is just a single command. mongo –eval “printjson(db.serverStatus())” Please note: if you are using Mongo operators, starting with a $ sign, you’ll want to surround the eval argument in single quotes to keep the shell from evaluating the operator as an environment variable: … Read more

How do I update/upsert a document in Mongoose?

Mongoose now supports this natively with findOneAndUpdate (calls MongoDB findAndModify). The upsert = true option creates the object if it doesn’t exist. defaults to false. var query = {‘username’: req.user.username}; req.newData.username = req.user.username; MyModel.findOneAndUpdate(query, req.newData, {upsert: true}, function(err, doc) { if (err) return res.send(500, {error: err}); return res.send(‘Succesfully saved.’); }); In older versions Mongoose does … Read more

Update MongoDB field using value of another field

The best way to do this is in version 4.2+ which allows using the aggregation pipeline in the update document and the updateOne, updateMany, or update(deprecated in most if not all languages drivers) collection methods. MongoDB 4.2+ Version 4.2 also introduced the $set pipeline stage operator, which is an alias for $addFields. I will use … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)