当前位置:网站首页>Some basic uses of mongodb

Some basic uses of mongodb

2022-06-26 00:13:00 Daily log of the great demon king

establish MongoDB Containers
docker run di --name=tianxingwen_mongodb -p 27017:27017 mongo
Remote login
mongo 192.168.53.132
Create database
use spitdb

Add, delete, change and look up the grammar

Inquire about

grammar :
db. Collection name .find()
db.spit.find()

according to id Query a piece of data
db.spit.find({"_id":“1”})
db.spit.findOne({"_id":“1”})

Query the first few data
db.spit.find().limit(3)

newly added

insert data
grammar :
db. Collection name .insert( data );
db.spit.insert({content:“ It's so cold ”,visits:10})

modify

grammar :
db. Collection name .update( Conditions , Revised data )
db.spit.update({_id:“1”},{visits:NumberInt(1000)})
Note that after the above command is executed id by 1 All other fields of the will be deleted automatically
terms of settlement :
db.spit.update({_id:“2”},{$set:{visits:NumberInt(2000)}})

Delete

grammar :
db. Collection name .remove( Conditions )
Delete all
db.spit.remove({})
Delete... According to conditions Delete visits:1000 This data of
db.spit.remove({visits:1000})

Statistics

Statistics of all
db.spit.count()
Statistics according to specified conditions
db.spit.count({userid:“1013”})

Fuzzy query

Inquire about content Field contains java All documents of
db.spit.find({content:/java/})
Inquire about content With java At the beginning
db.spit.find({content:/^java/})

Greater than less than

grammar :
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

Inquire about id>1 The data of
db.spit.find({"_id":{$gt:“1”}})

Inclusion and exclusion

Inquire about userid Field contains 1013,1014 Documents
db.spit.find({userid:{$in:[“1013”,“1014”]}})

Inquire about userid Field does not contain 1013,1014 Documents

db.spit.find({userid:{$nin:[“1013”,“1014”]}})

Conditional connection

Grammar format
$and:[ { },{ },{ } ]

Query collection visits Greater than or equal to 1000 And less than 2000 The article of

db.spit.find({ KaTeX parse error: Expected '}', got 'EOF' at end of input: and:[ {visits:{ gte:1000}} ,{visits:{$lt:2000} }]})

If the relationship between two or more conditions is or , We use Operators , With the front and Use The same way
KaTeX parse error: Expected '}', got 'EOF' at end of input: …*db.spit.find({ or:[ {userid:“1013”} ,{visits:{$lt:2000} }]})

Secondary title

Increase or decrease the value of a column on the basis of the original value , have access to KaTeX parse error: Expected '}', got 'EOF' at end of input: …ate({_id:"2"},{ inc:{visits:NumberInt(1)}} )**

原网站

版权声明
本文为[Daily log of the great demon king]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252125481909.html