创建
use DATABASE_NAME
//如果数据库不存在,则创建数据库,否则切换到指定数据库。
查询
db.getCollection(‘test’).find({})
db.test.find()
//等同于 select * from test
如果你想查看所有数据库,可以使用 show dbs 命令:
db.foo.find() // select * from foo
db.foo.find().limit(10) // select * from foo limit 10
db.foo.find().sort({x:1}) // select * from foo order by x asc 1:升序 -1:降序
db.foo.find().sort({x:1}).skip(5).limit(10) // select * from foo order by x asc limit 5, 10
db.foo.find({x:10}) // select * from foo where x = 10
db.foo.find({x: {$lt:10}}) // select * from foo where x <= 10
db.foo.find({}, {y:true}) // select y from foo
一些SQL不能做的,MongoDB也可以做:
db.foo.find({"address.city":"gz"}) // 搜索嵌套文档address中city值为gz的记录
db.foo.find({likes:"math"}) // 搜索数组
db.foo.ensureIndex({"address.city":1}) // 在嵌套文档的字段上建索引
这里都有,我就不写了:
https://www.runoob.com/mongodb/mongodb-create-collection.html