如果你的mongdb数据库已经启动,那就赶快上手操作吧,mongodb支持的查询非常丰富,可以点击方官方文档查看。
通过mongoshell连接数据库。Mongodb CRUD官方文档地址
创建集合并插入数据
和mysql相比,mongodb并不用提前创建数据库和集合,如果有test_jia数据库,使用该数据库,如果没有在我们插入inventory表数据时,创建test_jia数据库和inventory表。
> use test_jia //切换到test_jia数据库
switched to db test_jia
//使用insertOne将一个新文档插入到库存集合中,如果文档没有指定id字段,MongoDB将带有ObjectId值的id字段添加到新文档中
> db.inventory.insertOne(
... { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
... )
{
"acknowledged" : true,
"insertedId" : ObjectId("62be95a733d7f5c920972fd5")
}
//使用insertmany将多条数据插入到集合中,我们三条数据和之前的一条数据一样,但是也会插入成功
> db.inventory.insertMany([
... { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } },
... { item: "canvas", qty: 10, tags: ["cotton"], size: { h: 29, w: 37.5, uom: "cm" } },
... { item: "canvas", qty: 20, tags: ["cotton"], size: { h: 30, w: 36.5, uom: "cm" } }
... ])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("62be9b0d33d7f5c920972fd6"),
ObjectId("62be9b0d33d7f5c920972fd7"),
ObjectId("62be9b0d33d7f5c920972fd8")
]
}
查看集合数据
"_id"字段是集合默认的主键,可以通过命令 db.inventory.getIndexes()查看索引信息。
//使用find命令查看inventory集合包含字段【item: "canvas"】数据。我们看到自动添加了ObjectId值的id字段。
> db.inventory.find( { item: "canvas" } )
{ "_id" : ObjectId("62be95a733d7f5c920972fd5"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
>
//虽然前两条数据一样,但是默认生成的主键并没有重复索引也可以成功插入数据
> db.inventory.find( { item: "canvas" } )
{ "_id" : ObjectId("62be95a733d7f5c920972fd5"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd6"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd7"), "item" : "canvas", "qty" : 10, "tags" : [ "cotton" ], "size" : { "h" : 29, "w" : 37.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd8"), "item" : "canvas", "qty" : 20, "tags" : [ "cotton" ], "size" : { "h" : 30, "w" : 36.5, "uom" : "cm" } }
>
//也可以通过limit命令限制输出条数
> db.inventory.find( { item: "canvas" } ).limit(2)
{ "_id" : ObjectId("62be95a733d7f5c920972fd5"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd6"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
>
还可以使用比较操作符,使用方法官方原文
$eq 等于指定的值
$gt 大于指定的值
$get 大于等于指定的值
$in 选择字段值等于指定数组中任意值的文档
$lt 匹配小于指定值的值。
$lte 匹配小于或等于指定值的值。
$ns 匹配不等于指定值的所有值。
$min 不匹配数组中指定的任何值。
使用$in 查看字段qty值包含10或20的数据
> db.inventory.find( { qty: {$in : [10,20]}} )
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd7"), "item" : "canvas", "qty" : 10, "tags" : [ "cotton" ], "size" : { "h" : 29, "w" : 37.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd8"), "item" : "canvas", "qty" : 20, "tags" : [ "cotton" ], "size" : { "h" : 30, "w
使用sort进行排序,按照qty字段正序
> db.inventory.find().sort({"qty" : 1})
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd7"), "item" : "canvas", "qty" : 10, "tags" : [ "cotton" ], "size" : { "h" : 29, "w" : 37.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd8"), "item" : "canvas", "qty" : 20, "tags" : [ "cotton" ], "size" : { "h" : 30, "w" : 36.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be95a733d7f5c920972fd5"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd6"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
使用sort进行排序,按照qty字段倒序
> db.inventory.find().sort({"qty" : -1})
{ "_id" : ObjectId("62be95a733d7f5c920972fd5"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd6"), "item" : "canvas", "qty" : 100, "tags" : [ "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd8"), "item" : "canvas", "qty" : 20, "tags" : [ "cotton" ], "size" : { "h" : 30, "w" : 36.5, "uom" : "cm" } }
{ "_id" : ObjectId("62be9b0d33d7f5c920972fd7"), "item" : "canvas", "qty" : 10, "tags" : [ "cotton" ], "size" : { "h" : 29, "w" : 37.5, "uom" : "cm" } }
使用sort进行排序,按照qty字段倒序,然后skip方法,跳过前两行pretty更直观方式查看。
> db.inventory.find().sort({"qty" : -1}).skip(2).pretty()
{
"_id" : ObjectId("62be9b0d33d7f5c920972fd8"),
"item" : "canvas",
"qty" : 20,
"tags" : [
"cotton"
],
"size" : {
"h" : 30,
"w" : 36.5,
"uom" : "cm"
}
}
{
"_id" : ObjectId("62be9b0d33d7f5c920972fd7"),
"item" : "canvas",
"qty" : 10,
"tags" : [
"cotton"
],
"size" : {
"h" : 29,
"w" : 37.5,
"uom" : "cm"
}
}
查看数据库中的集合
> show tables;
inventory
>
count 统计集合行数
> db.inventory.count()
4
>
mongodb命令有很多,可以通过help命令查看帮助信息。
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
>
mongodb的CRUD--CR先到这里,update和del,下次分享。