Simple operation of Mongodb

Simple operation of Mongodb

 

【Introduction】

MongoDB is a database based on distributed file storage. Written in C++ language. It aims to provide scalable high-performance data storage solutions for WEB applications.

MongoDB is a product between relational databases and non-relational databases. It is the most feature-rich and most like relational databases among non-relational databases. The data structure it supports is very loose and is a JSON-like bson format, so it can store more complex data types.

The biggest feature of Mongo is that the query language it supports is very powerful. Its syntax is somewhat similar to the object-oriented query language. It can almost realize most functions similar to single-table query in relational databases, and it also supports indexing of data.

 

MongoDB is "collection-oriented", and data is grouped and stored in a dataset, called a collection. Each collection has a unique identifying name in the database and can contain an unlimited number of documents.

 

【basic concept】

Document: An ordered set of key-value pairs, such as {"age":25, "name":"John", "gender":"female"}--equivalent to a row in a table in a relational database (record, key is column, value is the value of each column)

Collection: A set of documents, equivalent to a table in a relational database

Database: A collection of multiple documents, which is equivalent to a relational database (database)

 

1. Operations on the database

#Query all databases

show dbs;

#View the database currently in use

db

db.getName()

#Switch or create a database, it will be automatically created when it does not exist, and it will be displayed in show dbs after the operation of creating a collection is required

use dbname

#The address of the current db connection

db.getMongo()

#Current db status

db.stats()

 

2. Operations on collections

#create collection

db.createCollection(“collName”, {size: 20, capped: 5, max: 100}); //The parameters in {} are optional, if not added, there is no capacity limit

# get the specified set

db.getColleciton("collName")

#得到当前db所有集合的名称

db.getCollectionNames()

 

3.对用户的操作

#显示当前db下的所有用户

show users

#添加用户

db.addUser("name");

db.addUser("userName", "pwd123", true);

#删除用户

db.removeUser("userName")

 

4.集合的查询(都是指当前db)

#查询当前db下指定集合的所有文档

db.collName.find()  ---联想:select * from collName;

#查询指定集合对某列去重之后的数据

db.collName.distinct("name") ---联想:select distinct name from collName;

#根据某列指定值查询

db.collName.find({"age": 25}) ---联想:select * from collName where age = 25;

#根据大于或等于查询

db.collName.find({"age": {$gt :20}}) --- age > 20

db.collName.find({"age": {$lt :20}}) --- age < 20

($gte则表示>=, $lte则表示<=)

#根据区间查询

db.collName.find({"age": {$gt :20, $lt:30}})  ---联想:select * from collName where age>20 and age<30;

#模糊匹配查询

db.collName.find({"name", /mongo/})  ---联想:select * from collName where name like '%mongo%';

db.collName.find({"name", /^mongo/}) ---联想:select * from collName where name like 'mongo%';(以……开头)

#查询指定列

db.collName.find({}, {name: 1, age: 1}); ---联想:select name, age from collName;

db.collName.find({age: {$gt: 25}}, {name: 1, age: 1});  ---联想:select name, age from collName where age > 25;(根据条件查询指定列)

#多条件查询

db.collName.find({name: 'Fanfan', age: 40});  ---联想:select * from collName where name = 'Fanfan' and age = 40;

#查询前几条

db.collName.find().limit(10)

#查询10条以后的

db.collName.find().skip(10)

#分页查询第5-10条

db.collName.find().limit(10).skip(5)

#查询结果数

db.collName.find({"age", {$lte:23}}).count()  ---联想:select count(1) from collName where age <= 23

#in, or 

db.collName.find({"user_id":{$in:["12345","123"]}})  ---联想: select * from collName where user_id in("12345", "123")

db.collName.find({$or: [{age: 22}, {age: 25}]});   ---联想:select * from collName where age = 22 or age = 25

($nin表示not in)

#按某列排序(-1表示降序)

db.collName.find().sort({age:1}) ---联想:select * from collName order by age (desc)

 

5.添加

db.collName.save({name: 'Lily', age: 25, gender: "female"});

db.collName.insert({name: 'Lily', age: 25, gender: "female"});

--联想:insert into collName(name, age, gender) values('Lily', 25, 'female');

 

6.修改

db.collName.update({age: 25}, {$set: {name: 'changeName'}}, false, true);   ---联想:update collName set name = ‘changeName’ where age = 25;

db.collName.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);   ---联想:update collName set age = age + 50 where name = ‘Lisi’;

db.collName.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);   ---联想:update collName set age = age + 50, name = ‘hoho’ where name = ‘Lisi’;

 

7.删除

db.collName.remove({age: 32});  ---联想:delete from collName where age = 32;

 

8.复合操作

db.collName.findAndModify({

    query: {age: {$gte: 25}}, 

    sort: {age: -1}, 

    update: {$set: {name: 'a2'}, $inc: {age: 2}},

    remove: true

});

 

db.runCommand({ findandmodify : "collName", 

    query: {age: {$gte: 25}}, 

    sort: {age: -1}, 

    update: {$set: {name: 'Angela'}, $inc: {age: 2}},

    remove: true

});

 

9.加索引

#加唯一索引

db.collection.ensureIndex({"chatId":1},{unique:true})

#加联合索引

db.collection.ensureIndex({"chatId":1, "pk":1})

 

10.其他

1)print("Hello World")  ---打印

2)for (var i = 0; i < 30; i++) {

... db.collName.save({name: "u_" + i, age: 22 + i, sex: i % 2});

... };---循环添加

3)var cursor = db.collName.find();

> while (cursor.hasNext()) { 

    printjson(cursor.next()); ---把集合的每条document打印成json格式的字符串

}---游标遍历

4)db.collName.find().forEach(printjson);---循环迭代

(find里面也可以加过滤条件)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486843&siteId=291194637