The installation

  1. Download the installation package
  2. Add the bin directory of mongodb to the global path variable
  3. Create the c:\data directory in the root directory and create the DB directory in data
  4. Command line operation
mongod.exe --dbpath c:\data\db
Copy the code

Start the service. 5. Run mongo.exe in the bin directory to open the shell command window of mongodb

The command line

  1. Creating a database
Use DATABASE_NAME// To switch to a database db// To display the current database name show DBS // to display all databasesCopy the code
  1. Deleting a Database
Db.dropdatabase (); db.collection.drop()Copy the code
  1. Inserted into the document
Db.collection_name. Insert (document); //COLLECTION_NAME does not exist; // Document can be a variable; The save() method is similar to the INSERT () method if the _ID field is not specified. If the _id field is specified, the _id data is updated.Copy the code
  1. Update the document
//query: query condition, Where //update:update object //upsert: optional, default false, meaning if no update record exists, insert //muti: optional, default false, update first or all Db.collection. update(<query>, <update>, {upsert:< Boolean >, multi:<bollean>, writeConcern:<document> } ) //save db.collection.save( <document>, { writeConcern:<document> } )Copy the code
  1. Delete the document
Db.collection. remove(<query>, {justOne:< Boolean >,// default false, delete all writeConcern:<document>})Copy the code
  1. Query the document
Db.collection. find(query, projection// Optional, using the projection operator to specify the key to return. Query returns all key values in the document, simply omit this parameter (default omitted). / / to display all the documents in the form of formatting the collections. The find (). Pretty () / / show only the first db collection. The findOne () / / $or [] {}, {},... Or:Copy the code
  1. Conditional operator
/ / $lt: less than / / $lte: less than or equal to the / / $gt: greater than / / $gte: greater than or equal to: / / $can not equal to zeroCopy the code
  1. The $type operators
type digital
Double 1
String 2
Object 3
Array 4
Binary data 5
Undefined(obsolete) 6
Object id 7
Boolean 8
Date 9
Null 10
Regilar Expression 11
JavaScript 13
Symbol 14
JavaScript(with scope) 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255
Max key integer 127
Db.collection. find({"title" : {$type: 2}})Copy the code
  1. Limit and skip
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
Copy the code
  1. sort
Db.collection_name. Find ().sort({KEY:1})Copy the code
  1. The index
Db.collection_name. EnsureIndex ({Key :1}); db.collection_name.Copy the code
  1. The aggregation
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Copy the code

Use advanced

  1. Duplicate database
db.source.find().forEach(fucntion(item){ db.target.insert(item) })

db.op_reply.find().forEach(function(item){ 
db.op_reply.update({_id:item._id}l,{$set:{messages:}})
Copy the code

}) 2. Change the messages structure

db.op_reply.find().forEach(function(item){ item.messages.forEach(function(it,index){ Item. Messages [index]={index:index,content:it}}) db.op_reply.save(item)}) //index starts from 0 db.op_reply.find().forEach(function(item){ item.messages.forEach(function(it,index){ item.messages[index]={index:index,content:it.content} }) db.op_reply.save(item) })Copy the code