This is the 15th day of my participation in Gwen Challenge
1. All available databases are displayed
> show dbs;
Copy the code
This command will display all of mongo’s database names and list them.
2. Switch databases
> use mydb;
Copy the code
This command selects a specified database or creates one automatically if one does not exist. Note, however, that the database has no data at this time, so when you run the show DBS command, you cannot see the database. It is visible only after the dataset has been inserted.
3. Display the data set
> show collections;
Copy the code
This command displays the dataset under the currently selected database. Note that if there is no data set, nothing is displayed.
4. Insert data
Insert data in the format db.{dataset name}. Insert ({data key value pair}), returns the number of inserts on success.
> db.test.insert({"name": "Island code farmer"});
WriteResult({ "nInserted": 1})Copy the code
Inserte multiple rows of data using brackets, and the result of the batch operation is returned, where nInserted returns the number of successful inserts.
> db.test.insert([{"name": "Island code farmer"}, {"name": "Nuggets"}]);
BulkWriteResult({
"writeErrors": []."writeConcernErrors": []."nInserted": 2."nUpserted": 0."nMatched": 0."nModified": 0."nRemoved": 0."upserted": []})Copy the code
5. Update data
The command to update a data is as follows, in the format of db.{dataset name}. Update ({query condition}, {$set: {updated data}}).
> db.test.update({"name": "Island code farmer"}, {$set: {"name": "Farmers"}});
WriteResult({ "nMatched" : 1, "nUpserted": 0."nModified": 1})Copy the code
The preceding command updates only one matching data. If you want to update multiple data sets, add the following parameter: {multi: true}.
> db.test.update({"name": "Island code farmer"}, {$set: {"name": "Farmers"}}, {multi: true});
WriteResult({ "nMatched": 2."nUpserted": 0."nModified": 2})Copy the code
Multiple updates can also be made using updateMany.
> db.test.updateMany({"name": "Farmers"}, {$set: {"name": "Island code farmer"}});
{ "acknowledged" : true."matchedCount": 3."modifiedCount": 3}Copy the code
6. Replace documents
Replacing a document replaces an existing document with a new document in the format db.{dataset name}.save({new document data}). For example, the following example replaces the file whose id is 60c8a50ADB9890BF41255FE4.
> db.test.save({"_id": "60c8a50adb9890bf41255fe4"."name": "Island Moron-1"});
WriteResult({
"nMatched": 0."nUpserted" : 1,
"nModified": 0."_id" : "60c8a50adb9890bf41255fe4"
})
Copy the code
7. Query data
The command to query data is in the format db.{dataset name}.find(). If you want to limit the number of entries, you can add limit(n).
> db.test.find();
Copy the code
Query out of the format need to beautify, add pretty() can be.
> db.test.find().pretty();
Copy the code
When querying by condition, add a filter parameter to find.
> db.test.find({"name":"Island code farmer"}).pretty();
Copy the code
8. Count the items
Use the count() function for statistics, or pass a filter in the find method if you need to filter.
> db.test.find().count();
Copy the code
9. Delete the document
Delete the document in db.test.remove({filter criteria}) format;
> db.test.remove({"name":"Island Moron-1"});
WriteResult({ "nRemoved": 1})Copy the code
The deleteOne method is used to deleteOne item, and the deleteMany method is used to delete multiple items.
> db.test.deleteOne({"name":"Island code farmer"});
{ "acknowledged" : true."deletedCount" : 1 }
> db.test.deleteMany({"name":"Island code farmer"});
{ "acknowledged" : true."deletedCount"2} :Copy the code
10. Check the help documentation
For some commands that do not understand the operation, check the operation document, the command format is db.{dataset name}.help().