db.userInfo.find();
select* from userInfo;
db.userInfo.distinct("name");
select distict name from userInfo;
db.userInfo.find({"age": 22});
select * from userInfo where age = 22;
db.userInfo.find({age: {$gt: 22}});
select * from userInfo where age > 22;
db.userInfo.find({age: {$lt: 22}});
select * from userInfo where age < 22;
db.userInfo.find({age: {$gte: 25}});
select * from userInfo where age >= 25;
db.userInfo.find({age: {$lte: 25}});
select * from userInfo where age <= 25;
db.userInfo.find({age: {$gte: 23, $lte: 25}});
select * from userInfo where age>=23 and age <= 25;
db.userInfo.find({age: {$ne: 25}});
select * from userInfo where age != 25;
db.userInfo.find({name: /mongo/});
select * from userInfo where name like '%mongo%';
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like 'mongo%';
db.userInfo.find({name: /mongo$/});
select * from userInfo where name like ‘%mongo’;
db.userInfo.find({}, {name: 1, age: 1});
select name, age from userInfo;
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
select name, age from userInfo where age > 25;
db.userInfo.find().sort({age: 1});
select * from userInfo order by age asc;
db.userInfo.find().sort({age: -1});
select * from userInfo order by age desc;
db.userInfo.find().limit(5);
select * from userInfo limit 5;
db.userInfo.find().skip(10);
db.userInfo.find().limit(10).skip(5);
select * from userInfo limit 5,5;
db.userInfo.find({name: 'zhangsan', age: 22});
select * from userInfo where name = 'zhangsan' and age = 22;
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
select * from userInfo where age = 22 or age = 25;
db.userInfo.find({age :{$in:[22,25]}});
select * from userInfo where age in (22,25);
db.userInfo.find({age: {$gte: 25}}).count();
select count(*) from userInfo where age >= 20;
db.userInfo.find({createTime:{$gt:ISODate("2020-11-09T00:00:00Z")}});
select * from userInfo where createTime> '2020-11-09 00:00:00';
db.userInfo.aggregate({$group:{_id:null,score:{$sum:"$score"}}})
SELECT SUM(score) from userInfo;
db.userInfo.aggregate({$group:{_id:null,score:{$avg:"$score"}}})
SELECT AVG(score) from userInfo;
db.userInfo.aggregate({$match:{createTime:{$gte:ISODate("2020-11-10T00:00:00Z"),$lt:ISODate("2020-11-11T00:00:00Z")}}},{$group:{_id:null,score:{$sum:"$score"}}})
SELECT SUM(score) from userInfo where createTime >= '2020-11-10 00:00:00' and createTime < '2020-11-11 00:00:00';
db.userInfo.insert([
{ "_id" : 1, "userId" : "xxxx", "username" : "ruink", "website" : "www.51ste.com" },
{ "_id" : 2, "userId" : "yyyy", "username" : "foosingy", "website" : "www.scgossip.com" }
])
db.userAdress.insert([
{ "_id" : 1, "userId" : "xxxx", address: "测试地址1"},
{ "_id" : 2, "userId" : "yyyy", address: "测试地址2"},
{ "_id" : 3, "userId" : "xxxx", address: "测试地址3"},
])
db.userInfo.aggregate([
{
$lookup:
{
from: "userAdress",
localField: "userId",
foreignField: "userId",
as: "address_detail"
}
},
{ $match : {"userId" :"xxxx"} }
])
[
{
_id: 1,
userId: 'xxxx',
username: 'ruink',
website:'www.51ste.com',
address_docs: [
{
_id: 1,
userId: 'xxxx',
address: '测试地址1'
},
{
_id: 3,
userId: 'xxxx',
address: '测试地址3'
}
]
}
]
文末寄语: 谁不向“前”看,谁就会面临许多困难。
发表评论