查询所有数据
db.songs.find();
查第一条
db.songs.findOne();
去重某列(分组)
db.songs.distinct("id");
等值查询
db.songs.find({"id":1});
db.songs.find({"_id":ObjectId("51c04ddbe30d4f079352009d")});
db.songs.find({"id":dfs}); (语法错误)
db.songs.find({"id":"dd"}); (匹配id是字符串)
非等值查询
db.songs.find({"id":{$gt:1}});
db.songs.find({"id":{$lt:2}});
db.songs.find({"id":{$gte:1}});
db.songs.find({"id":{$lte:2}});
db.songs.find({"id":{$lte:4,$gte:3}});
db.songs.find({"id":{$ne:665}});
包含查询
db.songs.find({"download_count":{$in:[3,34,33]}});
db.songs.find({"download_count":{$nin:[3,34,33]}});
db.songs.find({"download_count":{$all:[665]}});
db.songs.find({"name":/a/});(包含a)
db.songs.find({"name":/^a/});(以a开头)
查询结果指定列
db.songs.find({},{name:1}); (只显示name列)
db.songs.find({},{name:1,id:1}); (显示name和id两列)
排序(1:asc,-1:desc)
db.songs.find().sort({id:1});
db.songs.find().sort({id:-1});
db.songs.find().sort({"id":1,"name":-1});
分页 limit是pageSize,skip是第几页*pageSize
db.songs.find().sort({"id":-1}).limit(1);
db.songs.find().sort({"id":-1}).skip(1).limit(1);
OR查询
db.songs.find({$or:[{"id":1},{"id":2}]})
db.songs.find({$or:[{"id":1},{"id":{$gte:3}}]})
db.songs.find({$or:[{"id":{$gt:22}},{"category_id":{$in:[2,3,4,5,19]}}]});
And查询
db.songs.find({$and:[{"id":1},{"id":2}]})
db.songs.find({$and:[{"id":1},{"id":{$gte:3}}]})
db.songs.find({$and:[{"id":{$gt:22}},{"category_id":{$in:[2,3,4,5,19]}}]});
<>
db.songs.find({"name":{"$ne":"kebi"}});
in
db.songs.find({"age":{$in:[12,22,33]}});
选择特定列(0=false,!0=true)
db.songs.find({"age":{$in:[12,22,33]}},{age:0});
db.songs.find({"age":{$in:[12,22,33]}},{age:1});