Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb getting started command

Shulou Source: shulou.com Published: 2022-06-01 13:54:10 10月03日 Update

1: mongo getting started command

1.1: show dbs to view the current database

(admin is related to management, and user operations, security configuration, etc., need to be switched to this database. Local stores information from other databases. Test makes himself at will.)

1.2 use databaseName selection Library

1.2 show tables/collections View the collection under the current library (table)

Db.help () View help

1.3 how do I create a library?

The library of Mongodb is created implicitly, and you can use a library that does not exist

Then create a collection under the library to create the library

1.4 db.createCollection ('collectionName')

Create collection

1.5 collection allows implicit creation

Db.collectionName. × × ert (document)

Example:

Db.user. × × ert ({name:'lisi',age:18})

WriteResult ({"nInserted": 1})

Db.user. × × ert ({name:'zhangsan',age:22,gender:'male'})

WriteResult ({"nInserted": 1})

Db.user. × × ert ({name:'tingting',age:20,gender:'female',bobby: ['sleep','shopping']})

WriteResult ({"nInserted": 1})

Db.user.find ()

{"_ id": ObjectId ("5c8ce520a8f7096660e878a3"), "name": "lisi", "age": 18}

{"_ id": ObjectId ("5c8ce53aa8f7096660e878a4"), "name": "zhangsan", "age": 22, "gender": "male"}

{"_ id": ObjectId ("5c8ce669a8f7096660e878a5"), "name": "tingting", "age": 20, "gender": "female", "bobby": ["sleep", "shopping"]}

(of course you can customize _ id, but.)

1.6 db.collectionName.drop ()

Delete collection

1.7 db.dropDatabase (); (delete the current database)

Delete database

/ / Select a library

Use testing_base

/ / create a table

Db.createCollection ("the_table")

/ / single insert data

Db.getCollection ("the_table"). × × ert ({"name": "Zhang San", "age": 16, "address": "Chang'an Street"})

/ / insert data in bulk:

Db.getCollection ("the_table"). × × × ertMany ([

{"name": "Zhu Xiaoer", "age": 20, "address": "Beijing"}

{"name": "Zhang San Mad", "age": 50, "address": "Hubei"}

{"name": "Murong Hei", "age": 30, "address": "Hebei"}

])

/ / query all

Db.the_table.find ()

Db.getCollection (the_table) .find ()

/ / query the specified

Db.the_table.find ({name:' I'm going to change his name'})

Db.getCollection ("the_table") .find ({"name": "Zhang San", "age": 16})

/ / $gt: greater than

/ / $gte: greater than or equal to

/ / $lt: less than

/ / $lte: less than or equal to

/ / $ne: not equal to

/ / greater than or equal to

Db.getCollection ("the_table") .find ({"age": {"$gte": 30}})

/ / greater than or equal to 20, less than or equal to 30

Db.getCollection ("the_table") .find ({"age": {"$gte": 20, "$lte": 30}})

/ / query data for which "age" is greater than 21 and less than or equal to 24, and "name" is not "Zhu Xiaoer"

Db.getCollection ("the_table") .find ({"age": {"$gt": 21, "$lte": 30}, "name": {"$ne": "Zhu Xiaoer"}})

/ / db.getCollection ('table_name') .find (a dictionary for filtering records and a dictionary for qualifying fields)

/ / if there is no specific setting, _ id will be displayed by default

/ / if "id" is not considered, the values in the dictionary of the qualified field can only be all 0 or all 1, and it is impossible to mix l with 0. Once mixed, M ongoDB will report an error.

/ / do not query fields address and age

Db.getCollection ("the_table") .find ({}, {"address": 0, "age": 0})

/ / if only name field and age field are returned, the query statement is as follows:

Db.getCollection ("the_table") .find ({}, {"address": 1, "age": 1})

/ / usage of count

Db.getCollection ("the_table") .find ({"age": {"$gt": 10}}) .count ()

/ / limit the returned result limit (limit (1) returns the first entry by default)

Db.getCollection ("the_table"). Find (). Limit (1)

/ / sort sort for a pair of results (the value of the field is-l for reverse order and l for positive order)

Db.getCollection ("the_table") .find ({"age": {"$gt": 10}}) .sort ({"age":-1})

/ / Update operation

/ / updateOne: update only those data that meet the requirements in Article l.

/ / updateMany: update all the data that meets the requirements.

/ / modify the document where "name" is "Zhang Sanlian", add the "dizhi" field (no, just add), and change the "age" segment from "50" to "123".

Db.getCollection ("the_table") .updateMany ({"name": "Zhang San Mad"}, {"$set": {"dizhi": "Wudang", "age": 123}})

/ / Delete data (replace find with delete/deleteMany)

/ / use deletion function with caution. In general, logical deletion will be used in general projects. For example, add a del field to the document. If 0 means deletion, 1 will not delete.

/ / "delete" (if only the data in Article 1 that meets the requirements is deleted)

/ / "deleteMany" (if you want to delete all data that meets the requirements >

/ / Delete all data with an age of 666

Db.getCollection ("the_table") .deleteMany ({"age": 666})

/ / data deduplication

/ / db.getCollection ('the_table') .distinct (' field name', the first dictionary of the query statement)

/ / distinct () can take two parameters:

/ / the first parameter is the field name, indicating which field to deduplicate.

/ / the second argument is the 1st argument of the query command "find ()". The second argument of the distinct command can be omitted.

Db.getCollection ("the_table") .distinct ("name"); / / (the returned value is an array, which is the value after being deduplicated)

/ / de-duplicate the name field of a pair of records whose age is greater than 10

Db.getCollection ("the_table") .distinct ("name", {"age": {"$gt": 10}})

/ / can I repeat it and bring other fields with me? The answer is, but not with the "distinct ()" command.

Tags: Data fields queries parameters Zhang San commands dictionaries databases updates all that is documents results statements security two examples information reverse order functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi MariaDB Microsoft Huawei