The installation
- Download the installation package
- Add the bin directory of mongodb to the global path variable
- Create the c:\data directory in the root directory and create the DB directory in data
- 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
- 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
- Deleting a Database
Db.dropdatabase (); db.collection.drop()Copy the code
- 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
- 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
- Delete the document
Db.collection. remove(<query>, {justOne:< Boolean >,// default false, delete all writeConcern:<document>})Copy the code
- 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
- 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
- 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
- Limit and skip
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
Copy the code
- sort
Db.collection_name. Find ().sort({KEY:1})Copy the code
- The index
Db.collection_name. EnsureIndex ({Key :1}); db.collection_name.Copy the code
- The aggregation
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Copy the code
Use advanced
- 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