1024programmer Java mongodb basic addition, deletion, modification and query

mongodb basic addition, deletion, modification and query

To learn a database, the first thing you need to know is how to connect to the database, how to perform basic additions, deletions, modifications and queries, and you can learn other advanced functions slowly. The following mainly explains the basic addition, deletion, modification and query of mogodb 1. Create a database: use mkyongdb (jump to this database if there is one, create and jump if not) Display all databases: show dbs 2. Create a collection (

To learn a database, the first thing you need to know is how to connect to the database, how to perform basic additions, deletions, modifications and queries, and you can learn other advanced functions slowly.

The following mainly explains the basic addition, deletion, modification and query of mogodb

1. Create a database: use mkyongdb (if there is one, jump to this database, if not, create and jump)
Show all databases: show dbs
2. Create a collection (equivalent to a table in a relational database):
Collection operations:
(1) Insert data
db.users.insert({‘name’:’xumingxiang’,’sex’:’man’}) creates a collection of users
Show all collections under the current database: show collections
Display all data documents under the users collection: db.users.find()
Display the number of records in the collection: db.users.count()
(2) Update data
db.users.update({‘name’:’xiangshu’},{‘$set’:{‘sex’:’women’}},upsert=true,multi=false)
First: Query conditions
Second: Updated fields
Third: insert
if it does not exist
Fourth: Whether to allow modification of multiple records
(3)Delete data
db.users.remove({‘name’:’xumingxiang’})
(4) Delete all records
db.users.remove()
Delete collection
db.users.drop() //If the deletion is successful, it will return “true”, otherwise it will return “false”
Delete current database
db.dropDatabase()

Comparison of NOSQL and SQL databases (PS: refer to the Internet)
MongoDB syntax MySql syntax
db.test.find({‘name’:’foobar’}) select * from test where name=’foobar’
db.test.find() select * from test
db.test.find({‘ID’:10}).count() select count(*) from test where ID=10
db.test.find().skip(10).limit(20) select * from test limit 10,20
db.test.find({‘ID’:{$in:[25,35,45]}}) select * from test where ID in (25,35,45)
db.test.find().sort({‘ID’:-1}) select * from test order by ID desc
db.test.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name) from test where ID<20
db.test.group({key:{‘name’:true},cond:{‘name’:’foo’},reduce:function(obj,prev){prev.msum+=obj.marks;} ,initial:{msum:0}}) select name,sum(marks) from test group by name
db.test.find(‘this.ID<20',{name:1}) select name from test where ID<20
db.test.insert({‘name’:’foobar’,’age’:25})insert into test (‘name’,’age’) values(‘foobar’,25)
db.test.remove({}) delete * from test
db.test.remove({‘age’:20}) delete test where age=20
db.test.remove({‘age’:{$lt:20}}) elete test where age<20
db.test.remove({‘age’:{$lte:20}}) delete test where age<=20
db.test.remove({‘age’:{$gt:20}}) delete test where age>20
db.test.remove({‘age’:{$gte:20}}) delete test where age>=20
db.test.remove({‘age’:{$ne:20}}) delete test where age!=20
db.test.update({‘name’:’foobar’},{$set:{‘age’:36}}) update test set age=36 where name=’foobar’
db.test.update({‘name’:’foobar’},{$inc:{‘age’:3}}) update test set age=age+3 where name=’foobar’

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-basic-addition-deletion-modification-and-query/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索