1024programmer Mongodb Basic statement usage method of MongoDB database

Basic statement usage method of MongoDB database

Query:

MySQL:

SELECT * FROM user

Mongo:

db. user. find()

Conditional query:

MySQL:

SELECT * FROM user WHERE name = ’starlee’

Mongo:

db.user.find({‘name’ : ’starlee’})

Insert:

MySQL:

INSERT INOT user (`name`, `age`) values ​​(‘starlee’,25)

Mongo:

db.user.insert({‘name’ : ’starlee’, ‘age’ : 25})

If you want to add a field in MySQL, you must:

ALTER TABLE user….

But in MongoDB you just need:

db.user.insert({‘name’ : ‘starlee’, ‘age’ : 25, ’email’ : ‘ [email protected]’})

Delete:

MySQL:

DELETE * FROM user

Mongo:

db.user.remove({})

Conditional deletion:

MySQL:

DELETE FROM user WHERE age < 30

Mongo:

db.user.remove({‘age’ : {$lt : 30}})

MongoDBSize comparison symbols in :

$gt : >

$gte : >=

$lt : <

$ lte : <=

$ne : !=

UPDATEUpdate:

MySQL:

UPDATE user SET `age` = 36 WHERE `name` = ’starlee’

Mongo:

db.user.update({‘name’ : ’starlee’}, {$set : {‘age’ : 36}})

Update with operations:

MySQL:

UPDATE user SET `age` = `age` + 3 WHERE `name` = ‘starlee’

Mongo:

db.user.update({‘name’ : ’starlee’}, {$inc : {‘age’ : 3}})

countTotal:

MySQL:

SELECT COUNT(*) FROM user WHERE `name` = ’starlee’

Mongo:

db.user.find({‘name’ : ’starlee’}).count()

It can also be written like this:

db.user.count({‘name’:’starlee’})

LIMITclause:

MySQL:

SELECT * FROM user limit 10,20

Mongo:

db.user.find().skip(10).limit(20)

INclause:

MySQL:

SELECT * FROM user WHERE `age` IN (25, 35,45)

Mongo:

db.user.find({‘age’ : {$in : [25, 35, 45]}})

OrderSort:

MySQL:

SELECT * FROM user ORDER BY age DESC

Mongo:

db.user.find().sort({‘age’ : -1})

DistinctDistinct:

MySQL:

SELECT DISTINCT(name) FROM user WHERE age > 20

Mongo:

db.user.distinct(‘name’, {‘age’: {$lt : 20}})

Group Group:

MySQL:

SELECT name, sum(marks) FROM user where name=’foo’ GROUP BY name

Mongo:

db.user.group({

key : {‘name’ : true},

cond: {‘name’ : ‘foo’},

reduce: function(obj,prev) { prev.msum += obj.marks; },

initial: {msum : 0}

});

Get the namefield of the data whose agefield is less than 20

MySQL:

SELECT name FROM user WHERE age < 20

Mongo:

db.user.find(‘this.age < 20’, {name : 1})

Cycle insert data:

for(var i=0;i<100;i++)db.user.insert({uid:i,uname:'nosqlfan'+i});

In this way, one hundred pieces of data are inserted at one time

{ “_id” : ObjectId(“4c876e519e86023a30dde6b8″), “uid” : 55, “uname” : “nosqlfan55″ }

{ “_id” : ObjectId(“4c876e519e86023a30dde6b9″), “uid” : 56, “uname” : “nosqlfan56″ }

{ “_id” : ObjectId(“4c876e519e86023a30dde6ba”), “uid” : 57, “uname” : “nosqlfan57″ }

{ “_id” : ObjectId(“4c876e519e86023a30dde6bb”), “uid” : 58, “uname” : “nosqlfan58″ }

{ “_id” : ObjectId(“4c876e519e86023a30dde6bc”), “uid” : 59, “uname” : “nosqlfan59″ }

{ “_id” : ObjectId(“4c876e519e86023a30dde6bd”), “uid” : 60, “uname” :“nosqlfan60″ }

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/basic-statement-usage-method-of-mongodb-database/

author: admin

Previous article
Next article

Leave a Reply

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

The latest and most comprehensive programming knowledge, all in 1024programmer.com

© 2023 1024programmer - Encyclopedia of Programming Field
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

首页
微信
电话
搜索