CMD mongodb - bigktmbig/MyExperienceAlgorithm GitHub Wiki

  • mongod --dbpath=yourdirectory\data\db

  • connect: name: localhost string: mongodb://127.0.0.1:27017

  • export collection: mongoexport --db test --collection traffic --out traffic.json

  • import collection: mongoimport --db test --collection traffic --file contacts.json

  • export db: mongodump --db cit_room_chat --out D:\

  • restore db: mongorestore --db cit_room_chat D:\cit_room_chat

  • run command line mode mongodb:->mongo

  • check current db:->db

  • use db(db name: 'bigdb'):->use bigdb

  • show all db:->show dbs

  • drop current db you are using:->db.dropDatabase()

  • show all collection:->show collections

  • create collection(collection name: 'bigcollection'):->db.createCollection("bigclollection")

  • drop collection(collection name: 'bigcollection'):->db.bigcollection.drop()

  • insert field for all document: db.bigcollection.update({}, {$set: {isGolden: false}}, {multi: true})

  • remove a field for all document: db.bigcollection.update({}, {$unset: {"big_field": ''}}, {multi: true})

  • insert a object in a array object: db.bigcollection.update({},{$push:{ "friends" : {"status" : 0} }},{ multi: true })

  • insert empty array: db.bigcollection.update({},{$set:{ "friends" : []}},{ multi: true });

  • db.pbses.update({}, {$rename: {"total": "total_all"}}, false, true);

  • db.pbses.find().snapshot().forEach( function (x) { x.services.price_si_usd = x.services.price; delete x.services.price; db.pbses.save(x); });

  • db.pbsses.find({ 'services.0.price': { $exists: 1 } }).snapshot().forEach(function(item){for(i = 0; i != item.services.length; ++i){item.services[i].price_si_usd = item.services[i].price;delete item.services[i].price;}db.pbsses.update({_id: item._id}, item);});

  • find follow codition: db.collection.find( { qty: { $gt: 4 } } )

  • Datatype in mongodb:

  • String βˆ’ This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid.

  • Integer βˆ’ This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.

  • Boolean βˆ’ This type is used to store a boolean (true/ false) value.

  • Double βˆ’ This type is used to store floating point values.

  • Min/ Max keys βˆ’ This type is used to compare a value against the lowest and highest BSON elements.

  • Arrays βˆ’ This type is used to store arrays or list or multiple values into one key.

  • Timestamp βˆ’ ctimestamp. This can be handy for recording when a document has been modified or added.

  • Object βˆ’ This datatype is used for embedded documents.

  • Null βˆ’ This type is used to store a Null value.

  • Symbol βˆ’ This datatype is used identically to a string; however, it's generally reserved for languages that use a specific symbol type.

  • Date βˆ’ This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it.

  • Object ID βˆ’ This datatype is used to store the document’s ID.

  • Binary data βˆ’ This datatype is used to store binary data.

  • Code βˆ’ This datatype is used to store JavaScript code into the document.

  • Regular expression βˆ’ This datatype is used to store regular expression.