当前位置:网站首页>Mongodb common commands
Mongodb common commands
2022-07-26 05:24:00 【Monster in the North Sea】
Select and create databases
use Database name
If the database exists, select it , If the database does not exist, it will be created automatically . The following statement creates
commentdb database :
use commentdb
view the database :show dbs
But you won't see the database you created , In essence, it has not been created , Only after you actually perform the write operation can you create .
View set , You need to select the database first , To view the collection of this database :show collections
After the database is created , You don't have to create collections , Insert data directly ,
Insert and query documents
After selecting the database , Use collections to manipulate documents , Insert document syntax format :
db. Collection name .insert( data );
Insert the following test data :
db.comment.insert({content:“ The tenth power course ”,userid:“1011”})
The syntax format of the query set :
db. Collection name .find()
Inquire about spit All documents in the collection , Enter the following command :
db.comment.find()
There will be a file called _id Field of , This is equivalent to the primary key of the table in our original relational database , This field is not specified when you insert a document record ,MongoDB Automatically created , The type is ObjectID type . If we specify this field when inserting a document record, we can also , Its type can be ObjectID type , It can also be MongoDB Any type supported .
Test statement
db.comment.insert({
_id:"1",content:" Why did it go wrong ",userid:"1012",thumbup:2020});
db.comment.insert({
_id:"2",content:" Work overtime until midnight ",userid:"1013",thumbup:1023});
db.comment.insert({
_id:"3",content:" What if the mobile traffic exceeds ",userid:"1013",thumbup:111});
db.comment.insert({
_id:"4",content:" perseverance prevails ",userid:"1014",thumbup:1223});

In the same set , Data composition can be different .
Query according to certain conditions , Such as query userid by 1013 The record of , As long as find() Add parameters to , So are the parameters json Format , as follows :
db.comment.find({userid:‘1013’})
Just return the first data that meets the conditions , We can use findOne Command to implement :
db.comment.findOne({userid:‘1013’})
Return the specified number of records , Can be in find After the method is called limit To return results , for example :
db.comment.find().limit(2)
Modifying and deleting documents
Modify the syntax structure of the document :
db. Collection name .update( Conditions , Revised data )
modify _id by 1 The record of , Thumb up for 1000, Enter the following statement :
db.comment.update({_id:“123”},{thumbup:1000})
It was found after execution that , This document is in addition to thumbup The other fields are missing .
To solve this problem , We need to use modifiers $set To achieve , The order is as follows :
db.comment.update({_id:“123”},{$set:{“name”:“ Zhang San ”}})
This field can be modified if any , Add this field without 
Delete the syntax structure of the document :
db. Collection name .remove( Conditions )
The following statement can delete all the data , Use with caution ~ Delete all data
db. Collection name .remove({})
Delete conditions can be placed in braces , Delete... For example thumbup by 1000 The data of , Enter the following statement :
db. Collection name .remove({thumbup:1000})
Count the number
Use of statistical record conditions count() Method . Statistics of the following statements spit The number of records in the collection :
db. Collection name .count()
Statistics by condition , For example, statistics userid by 1013 The number of records :
db.comment.count({userid:“1013”})
Fuzzy query
MongoDB The fuzzy query is realized by regular expression . The format is :
/ Fuzzy query string /
Query comments contain “ Traffic ” All documents of , The code is as follows :
db.comment.find({content:/ Traffic /})
Query comments with “ Work overtime ” At the beginning ,
db.comment.find({content:/^ Work overtime /})
Query comments with “ Work overtime ” At the end of the ,
db.comment.find({content:/ Work overtime $/})
Greater than Less than It's not equal to
db. Collection name .find({ “field” : { $gt: value }}) // Greater than : field > value
db. Collection name .find({ “field” : { $lt: value }}) // Less than : field < value
db. Collection name .find({ “field” : { $gte: value }}) // Greater than or equal to : field >= value
db. Collection name .find({ “field” : { $lte: value }}) // Less than or equal to : field <= value
db. Collection name .find({ “field” : { $ne: value }}) // It's not equal to : field != value
The number of query comment likes is greater than 1000 The record of :db.comment.find({thumbup:{$gt:1000}})
Inclusion and exclusion
Include use $in The operator
Query the comments collection userid Field contains 1013 and 1014 Documents :
db.comment.find({userid:{$in:[“1013”,“1014”]}})
Does not include the use of KaTeX parse error: Expected '}', got 'EOF' at end of input: ….find({userid:{ nin:[“1013”,“1014”]}})
Conditional connection
If we need to satisfy the above two conditions at the same time , Need to use $and Operator to associate conditions ( amount to SQL Of and). The format is :
KaTeX parse error: Expected '}', got 'EOF' at end of input: ….comment.find({ and:[ {thumbup:{ KaTeX parse error: Expected 'EOF', got '}' at position 9: gte:1000}̲} ,{thumbup:{ lt:2000} }]})
If the relationship between two or more conditions is or , We use operators for association , With the front and It's the same way , The format is :
KaTeX parse error: Expected '}', got 'EOF' at end of input: ….comment.find({ or:[ {userid:“1013”} ,{thumbup:{$lt:2000} }]})
Column value growth
Increase or decrease the value of a column on the basis of the original value , have access to $inc Operator : To give a negative number is to reduce
db.comment.update({_id:“2”},{$inc:{thumbup:1}})
边栏推荐
- DOM操作--操作节点
- Shell的read 读取控制台输入、read的使用
- 35. 搜索插入位置
- SIP账号注册的SIP软电话的使用和常见问题
- 推荐必读:测试人员如何快速熟悉新业务?
- Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)
- Why is the value represented by a negative number greater than an integer by 1?
- Code audit CMS
- MySQL master-slave synchronization and master-slave synchronization delay solution
- When AQS wakes up the thread, I understand why it traverses from the back to the front
猜你喜欢
随机推荐
Do you really understand fiddler, a necessary tool for testing?
IVR在voip电话系统的应用与价值
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
TZC 1283: simple sort Bubble Sort
ThreadLocal transfer between parent and child threads in asynchronous
Shell的read 读取控制台输入、read的使用
怎么办理聚合收款码
ALV入门
MySQL master-slave synchronization and master-slave synchronization delay solution
ABAP grammar learning (ALV)
Hack The Box - Introduction to Networking Module详细讲解中文教程
嵌入式开发小记,实用小知识分享
ABAP语法学习(ALV)
Hack The Box -SQL Injection Fundamentals Module详细讲解中文教程
数仓搭建-DIM层
开发转测试:从零开始的6年自动化之路
Embedded sharing collection 21
Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)
DOM event flow event bubble event capture event delegate
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output









