Posts

Showing posts with the label Mongodb

Get distinct value in MongDB

 db.the_collection_name.distinct('the_field_name')   To get count of distinct value var collection = "the_collection_name"; var field = "the_field_name"; db[collection].distinct(field).forEach(     function(value) {         print(field + ", " + value + ": " + db[collection].count({[field]: value}))     } )

Count number of occurrences in MongoDB

 db.collection_name.aggregate(    {$group : { _id : '$field_name', count : {$sum : 1}}},    {$sort: { count: -1 } } )

List all fields in MongoDB collection

var task = db.collection_name.find().next(); for (let key in task){print(key)};

Run MongoDB query form terminal

 mongo localhost:27017/db_name script_name.js > result

Move MongDB database

find mongodb config file sudo find / -name mongod.conf -print get dbPath from that file stop mongodb service mongod stop check mongodb is not running sudo systemctl status mongod copy all files from dbPath to new dbPath cp -r old_dbPath new_dbPath chown of new dbPath sudo chown -R mongodb:mongodb new_dbPath change dbPath in mongodb config file to new_dbPath start mongodb sudo systemctl start mongod

Get _id of document in Mongodb using PHP

$documentObject["_id"]->{'$id'}