MongoDB Shell入门例子
MongoDB Shell
db命令【创建切换数据库】
- db是指您当前的数据库
- 要切换数据库,请键入。use
examples
切换之前,无需创建数据库。当您第一次在数据库中存储数据时(例如在数据库中创建第一个集合),MongoDB会创建数据库。
> db
test
> use mydb
switched to db mydb
> db
mydb
插入表数据
- 集合类似于关系数据库中的表。如果不存在集合,则在您第一次为该集合存储数据时,MongoDB会创建该集合。
- 以下示例使用该
db.collection.insertMany()
方法将新 文档插入到inventory
集合中
> db.inventory.insertMany([
... { item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] },
... { item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank" ] },
... { item: "paper", qty: 10, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank", "plain" ] },
... { item: "planner", qty: 0, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: [ "blank", "red" ] },
... { item: "postcard", qty: 45, status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] }
... ]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5ebe42508073754f2e3f11b3"),
ObjectId("5ebe42508073754f2e3f11b4"),
ObjectId("5ebe42508073754f2e3f11b5"),
ObjectId("5ebe42508073754f2e3f11b6"),
ObjectId("5ebe42508073754f2e3f11b7")
]
}
查询文档
- 查询所有
db.inventory.find({})
db.inventory.find({}).pretty()
要格式化结果,请将追加.pretty()到 find操作:
> db.inventory.find({})
{ "_id" : ObjectId("5ebe42508073754f2e3f11b3"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b4"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b5"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b6"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b7"), "item" : "postcard", "qty" : 45, "status" : "A", "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "tags" : [ "blue" ] }
> db.inventory.find({}).pretty()
{
"_id" : ObjectId("5ebe42508073754f2e3f11b3"),
"item" : "journal",
"qty" : 25,
"status" : "A",
"size" : {
"h" : 14,
"w" : 21,
"uom" : "cm"
},
"tags" : [
"blank",
"red"
]
}
{
"_id" : ObjectId("5ebe42508073754f2e3f11b4"),
"item" : "notebook",
"qty" : 50,
"status" : "A",
"size" : {
"h" : 8.5,
"w" : 11,
"uom" : "in"
},
"tags" : [
"red",
"blank"
]
}
{
"_id" : ObjectId("5ebe42508073754f2e3f11b5"),
"item" : "paper",
"qty" : 10,
"status" : "D",
"size" : {
"h" : 8.5,
"w" : 11,
"uom" : "in"
},
"tags" : [
"red",
"blank",
"plain"
]
}
{
"_id" : ObjectId("5ebe42508073754f2e3f11b6"),
"item" : "planner",
"qty" : 0,
"status" : "D",
"size" : {
"h" : 22.85,
"w" : 30,
"uom" : "cm"
},
"tags" : [
"blank",
"red"
]
}
{
"_id" : ObjectId("5ebe42508073754f2e3f11b7"),
"item" : "postcard",
"qty" : 45,
"status" : "A",
"size" : {
"h" : 10,
"w" : 15.25,
"uom" : "cm"
},
"tags" : [
"blue"
]
}
>
条件查找
对于相等匹配(即
equals
),在查询过滤器文档中指定并传递给该 方法。db.collection.find()
- 多条件查找
> db.inventory.find( { qty: 0, status: "D" } );
{ "_id" : ObjectId("5ebe42508073754f2e3f11b6"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
- 嵌套条件查找
> db.inventory.find( { "size.uom": "in" } )
{ "_id" : ObjectId("5ebe42508073754f2e3f11b4"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b5"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
-- 嵌入式文档上的相等匹配要求完全匹配,包括字段顺序。
> db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
{ "_id" : ObjectId("5ebe42508073754f2e3f11b3"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
- 数组查找
其中tags数组包含"red"为其元素之一,如果该tags字段是字符串而不是数组,则查询只是一个相等匹配。
> db.inventory.find( { tags: "red" } )
{ "_id" : ObjectId("5ebe42508073754f2e3f11b3"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b4"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b5"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b6"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
-- 以返回该tags字段与指定数组完全匹配的文档,包括顺序
> db.inventory.find( { tags: [ "red", "blank" ] } )
{ "_id" : ObjectId("5ebe42508073754f2e3f11b4"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
要返回的字段,默认返回所有
要指定要返回的字段,请将投影文档传递给该 方法。在投影文档中,指定:db.collection.find(
-
在返回的文档中包含一个字段: 1 -
在返回的文档中排除字段: 0
您不必指定_id字段即可返回该字段。默认情况下返回。要排除该字段,请0在投影文档中将其设置为 。
> db.inventory.find( { }, { item: 1, status: 1 } );
{ "_id" : ObjectId("5ebe42508073754f2e3f11b3"), "item" : "journal", "status" : "A" }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b4"), "item" : "notebook", "status" : "A" }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b5"), "item" : "paper", "status" : "D" }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b6"), "item" : "planner", "status" : "D" }
{ "_id" : ObjectId("5ebe42508073754f2e3f11b7"), "item" : "postcard", "status" : "A" }
-- 排除id字段
> db.inventory.find( {}, { _id: 0, item: 1, status: 1 } );
{ "item" : "journal", "status" : "A" }
{ "item" : "notebook", "status" : "A" }
{ "item" : "paper", "status" : "D" }
{ "item" : "planner", "status" : "D" }
{ "item" : "postcard", "status" : "A" }