MongoDB learning notes

Keywords: MongoDB

MongoDB learning notes

01 - start MongoDB

// Open service
sudo service mongodb start
// Start MongoDB
mongo

02 - database operation command

// View the database in use in the current period
db
// View all databases
show dbs
// Connect or create myTest database
use myTest
// Destroy myTest database
use myTest
db.dropDatabase()

03 - creation and deletion of collections

// Create the users collection in the mytest database
use myTest
db.createCollection("users")
// View collections in the myTest database
show collections
// Delete users collection
db.users.drop()

04 - insert data into set

If there is no corresponding set in the database, the corresponding set will be automatically created when inserting the set.

// Inserts a document into the users collection
db.users.insert([{"name":"LiSi"},{"name":"WangWu"}])
// Define the data before inserting it
LiSi=({"name":"lisi","email":"888qq.com"})
db.users.insert(LiSi)

05 - save or modify data in the set

// Save data to the users collection, or modify an existing document
db.users.save([{"name":"LiSi"},{"name":"WangWu"}])

06 - update data in set

// Change the name in the document content with the name lisi in the users collection to "wangwu"
db.users.update({"name":"lisi"},{$set:{"name":"wangwu"}},{multi:true})
  • The content in the first brace is the query criteria, and the content in the second brace indicates the data and content to be updated.
  • By default, update updates only one document. If you want to update all data, you need to add "{multi: true}"

07 query data in collection

// Query all documents in the users collection
db.users.find()
// Format and display the queried data
db.users.find().pretty()
// Find the document with name lgl
db.users.find({"name":"lgl"}).pretty()
// Find lgl with name and mailbox“ 123@qq.com "Document for"
db.users.find({"name":"lgl","email":"123@qq.com"})
// Find lgl with name or mailbox“ 666@qq.com "Document for"
db.users.find({
	$or:[
	{"name":"lgl"},
	{"email":"666@qq.com"}
	]
}).pretty()

08 conditional operator and its application

// Find users older than 18 and younger than 25 in the users collection
// And the name is "666" or the email is“ 123@qq.com "Document for"
db.users.find({
	"ages": {$gt:18,$lt:25},
    $or: [
		{"name": "666"},
		{"email": "123@qq.com"}
	]
}).pretty()
  • gt: greater than greater than
  • lt: less than
  • gte: greater than equal
  • lte: less than equal

09 fuzzy query

// Find documents whose mailbox starts with '123' in the users collection
db.users.find({"email":/^123/}).pretty()
// Find documents with names ending in 'l' in the users collection
db.users.find({"name":/l$/}).pretty()

Application of 10 type conditional operator

Syntax: $type: [key]

The optional key values are as follows:

  • 1: Double (Double)
  • 2: String (String)
  • 3: Object
  • 4: Array
  • 5: Binary data
  • 7: Object ID(Object id)
  • 8: Boolean type
  • 9: Date
  • 10: Empty (Null)
  • 11: Regular expression
  • 13: JS code (Javascript)
  • 14: Symbol
  • 15: JS code with scope (JavaScript with scope)
  • 16: 32-bit integer
  • 17: Timestamp
  • 18: 64 bit integer
  • -1: Min key
  • 127: max key
// Find a document with name type "string"
db.myTest.find({"name":{$type:2}})
// The above code is equivalent to:
db.myTest.find({"name":{$type:"string"}})

11 use of limit() and skip()

  • limit(n): limit the number of query output results to no more than n!
db.users.find().limit(3)
  • skip(m): skip the first m document data during output
// Skip the first three document data
db.users.find().skip(3)
// Skip the first three document data and limit the output document entries to no more than 3
db.users.find().limit(3).skip(3)

12 - sorting of output data

// Sort by name in ascending order
db.users.find().sort({"name":1})
// In sort: '1' indicates ascending order and '- 1' indicates descending order.

13 - create index

// Index Names
db.users.ensureIndex({"name":1})
// '1' indicates ascending order and '- 1' indicates descending order.

14 aggregate()

Syntax:

db.COLLECTION_NAME.aggregate([
	{$match:{x:1}},
	{$limit:NUM},
	{$skip:NUM},
	{$group:{_id:$age}},
	{$sort:{"doc_name":1}},
	{$limit:NUM},
	{$skip:NUM},
	{$match:{x:1}}
])

explain:

  • $match: query, the same as find;
  • $limit: limit the number of displayed results;
  • $skip: ignore the result quantity;
  • $sort: sort;
  • $group: combine the results according to the given expression (grouping and merging).

give an example:

// Count the number of documents with the same name in the users collection, and sort the results in ascending order
db.users.aggregate([
	{$group:{_id:"$name", user:{$sum:1}}},
	{$sort:{_id:1}}
])
// $name means to get the value of name.

15 - atomic operation

Atomic operation is either successful or failed. The execution succeeds in completing a given task, and the execution fails to restore the state before execution.

Common atomic operation commands:

01,$set

Used to specify a key and update the key value. If the key does not exist, it is created.

{ $set : { field : value } }

02,$unset

Used to delete a key.

{ $unset : { field : 1} }

03,$inc

$inc can increase or decrease a key whose value is numeric (only numbers that meet the requirements).

{ $inc : { field : value } }

04,$push

Add value to the field. The field must be an array type. If the field does not exist, an array type will be added.

{ $push : { field : value } }

05,$pushAll

The same as $push, but multiple values can be appended to an array field at one time.

{ $pushAll : { field : value_array } }

06,$pull

Deletes a value equal to value from the array field.

{ $pull : { field : _value } }

07,$addToSet

Add a value to the array, and only if the value is not in the array.

08,$pop

Deletes the first or last element of the array.

{ $pop : { field : 1 } }

10,$rename

Modify field name:

{ $rename : { old_field_name : new_field_name } }

11,$bit

Bit operation, integer type

{$bit : { field : {and : 5}}}

Posted by predator on Tue, 23 Nov 2021 12:40:29 -0800