Start the

Go to the MongoDB installation directory bin file and enter the mongod -dbpath /mongod -dbpath path of the db file

Homebrew for MAC:

Start: Brew Services start [email protected]

Close: Brew Services stop [email protected]

Background: the mongod — config/usr/local/etc/mongod. Conf – fork

See if run: the brew services list or ps aux | grep -v grep | grep mongod)

First, add, delete, change and check

To view all collections in the current database, use the

Show collections or use show tablesCopy the code

There are two ways to create a collection, explicit and implicit

Display creation can be created implicitly using the command db.createcollection (" collection name ") using the command db. Insert ({}), which creates a collection and inserts data into the collection at the same time, for example: db.customer.insert({name: "jack"})Copy the code

Add documents to the collection

Insert ({}), for example: db.user1.insert({name: "jack",age:20})Copy the code

Delete documents from the collection

Remove ({delete condition}) to delete all documents in the collection: Db.c1.remove () remove db.c1.remove({name: "user1"}) from c1Copy the code

Query documents in the collection

Db.collection name.find ({condition}) or use db.collection name.findone () to find the first documentCopy the code

Queries documents in a collection, returning some specific key values

In addition to query expressions, MongoDB supports some additional parameter options. Db.user. find({},{age:0}); db.user.find({},{age:0}); Return tags=tennis for all columns except comments db.posts.find({tags:'tennis'},{comments:0}); Db.user. find({userid:16},{name:1}); {16, "_id" : "name" : "user16}" returns all z fields of x = John db. Things. Find ({x: "John"}, {1} z:);Copy the code

Query documents in a collection using conditional expressions (<, <=, >, >=,! =)

Db.collection. find({field:{$gt:value}}); // less than: field < value db.collection.find({field:{$lt:value}}); Db.collection. find({field:{$gte:value}}); // Less than or equal to: field <= value db.collection.find({field:{$lte:value}}); // does not equal: field! = value db.collection.find({field:{$ne:value}});Copy the code

Query documents in collections, count, sort, skip, limit

db.customer.count(); db.customer.find().count(); db.customer.find({age:{$lt:5}}).count(); db.customer.find().sort({age:1}); Descending 1 db. The customer. The find (). Skip (2) limit (3); db.customer.find().sort({age:-1}).skip(2).limit(3); db.customer.find().sort({age:-1}).skip(2).limit(3).count(); db.customer.find().sort({age:-1}).skip(2).limit(3).count(0); db.customer.find().sort({age:-1}).skip(2).limit(3).count(1);Copy the code

Query documents in the collection

$in ($nin); $in ($in); $in ($in); $in ($in); $in ($in); Db.customer. find({$or:[{name: "user2"},{age:3}]}) $nor Db.customer. find({$nor:[{name: "user2"},{age:3}]}) $exists Db.customer. find({name:{$exists:1}}) $exists:1: true $exists:0: falseCopy the code

The cursor

Update the documents in the collection

Grammar: db. Collection. The update (criteria, objNew upsert, multi) parameters: ObjNew upsert: If the record already exists, update it; otherwise, add a new record. The value is 0 or 1. Multi: If there are multiple records that meet the criteria, whether to update all of them. Db.collection. update(criteria,objNew,0,1) change the name of the document in the set to user1 and name to jack: db.collection.update(criteria,objNew,0,1) Db.c1. update({name:"user1"},{name:"jack"}) $set specifies the value of a key and creates it if it does not exist. For example, to add address to a document whose name is user1, run the following command: Db.c1. update({name: "user1"},{$set:{address: "bj"}},0,1) update({$set:{address: "bj"}},0,1) Db.c1. update({name: "user1"},{$set:{address: "tj"}},0,1); Update ({name:"user1"},{$inc:{age:1}}) $unset db.c1.update({name:"user1"},{$inc:{age:1}}) $unset Db. C1. Update ({name: "user1"}, {$unset: {address: 1}}, 0, 1)Copy the code

Ii. Index:

Indexes are used to speed up queries. A database index is similar to a book index: it eliminates the need to go through the entire book, and the database can search directly in the index, making the search speed several orders of magnitude faster. Once you find the entry in the index, you can jump directly to the location of the target document.

Common index:

Create: db. Collection. EnsureIndex ({key: 1}) to check the related information about the index: the collection. The stats queries using the index () to view the situation: Db.collection.find ({key:value}).explain() delete index :db.collection.dropIndex({key:1}) delete index :db.collection.dropIndex({key:1}Copy the code

Unique index:

Create: db. Collection. EnsureIndex ({key: 1}. Db.collection.stats () :db.collection.find({key:value}).explain() DropIndex :db.collection.dropIndex({key:1}) dropIndex :db.collection.dropIndex({key:1}) dropIndexCopy the code

3. Capped Collection

A fixed collection is a collection that is created in advance and of a fixed size.

Fixed collection features: A fixed collection is much like a circular queue; if space runs out, the earliest documents are deleted to make room for new documents. In general, fixed collections work for any scenario where you want to automatically weed out expired attributes without too many operational restrictions.

Create a fixed collection:

Db. CreateCollection (" collectionName ", {capped: true, size: 100000, Max: 100}); Size specifies the collection size, in KB, and Max specifies the number of documentsCopy the code

When you specify a maximum number of documents, you must also specify the size. The elimination mechanism only works based on the number of documents if the capacity is not full. If capacity is full, the elimination mechanism works according to capacity.

Mongodump and mongorestore

MongoDB provides backup and restore functions, respectively mongodump.exe and mongorestore.exe files in MongoDB download directory (i.e., commands in MongoDB bin directory).

Back up data using the following command:

Mongodump -h dbhost -d dbname -o dbdirectory -h: address of the server where MongDB resides, for example, 127.0.0.1. You can also specify the port number 127.0.0.1:27017. -d: indicates the database instance to be backed up, for example: Test-o: indicates the location where the backup data is stored, for example, c:\data\dump. Of course, this directory needs to be created before the backup. After the backup is complete, the system automatically creates a test directory in the dump directory to store the backup data of the database instance.Copy the code

Restore data using the following command:

Mongorestore -h dbhost -d dbname -directoryperdb dbdirectory -h: MongoDB server address -d: database instance to be restored, for example: Test2 - Directoryperdb: indicates the location of the backup data, for example, c:\data\dump\testCopy the code

Mongoimport and export (mongoexport)

To export data, run the following command:

Mongoexport -h dbhost -d dbname -c collectionName -o output Mongoexport -h localhost:27017 -d test -c -o d:/beifeng/c4.txt example: mongoexport -h localhost:27017 -d test -c c4 -o d:/beifeng/c4.txtCopy the code

To import data, run the following command:

Mongoimport -h dbhost -d dbname -c collectionName -- mongoimport -h dbhost -d dbname -c collectionName Parameter description: -h database address -d library to be used -c Local file address of collection to be imported... Mongoimport -h localhost:27017 -d test -c CCTV d:/beifeng/c4.txtCopy the code