-
shell连接MongoDB
# mongo <host>/<db> -u <user> -p <password> mongo 192.168.1.200:27017/database -u user -p password -
切换数据库:切换数据库之前不需要创建数据库,当第一次在数据库中存储数据时,MongoDB会自动创建数据库。
# db指当前数据库,mongoDB默认是test数据库 db # use <db>用于切换数据库,比如说切换到examples数据库: use examples -
插入集合:MongoDB将文档存储在集合中。集合类似于关系数据库中的表,如果数据库中不存在该集合,在第一次为该集合存储数据时,MongoDB会自动创建集合。
db.collection.insertMany()可插入多条数据到集合中,如果_id字段在文档中不存在,MongoDB就使用ObjectId添加一个_id字段。该操作返回一个包含一个确认指示符acknowledged和包含每个成功插入的文档_id的数组文档。> 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("5e106095fa6c58a3116cee8b"), ObjectId("5e106095fa6c58a3116cee8c"), ObjectId("5e106095fa6c58a3116cee8d"), ObjectId("5e106095fa6c58a3116cee8e"), ObjectId("5e106095fa6c58a3116cee8f") ] } -
查找文档:
db.collection.find()用于从集合中查找文档,过滤条件传空用于查找所有的文档。find()操作后添加.pretty()可格式化输出。# 查找所有的文档 > db.inventory.find({}) { "_id" : ObjectId("5e106095fa6c58a3116cee8b"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8c"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8d"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8e"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8f"), "item" : "postcard", "qty" : 45, "status" : "A", "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "tags" : [ "blue" ] } # 格式化输出 > db.inventory.find({}).pretty() { "_id" : ObjectId("5e106095fa6c58a3116cee8b"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8c"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8d"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8e"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8f"), "item" : "postcard", "qty" : 45, "status" : "A", "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "tags" : [ "blue" ] } -
指定相等匹配:在
db.collection.find()方法查询时使用<field>: <value>形式可指定相等匹配条件。- 匹配的
field为字符串类型,value也为字符串,则为相等匹配,如db.inventory.find( { status: "D" } )表示status等于D的文档记录。 - 匹配的
field为嵌入式文档类型,value也为嵌入式文档类型,则也为相等匹配,嵌入式文档的匹配要求完全匹配,包括嵌入式文档中的字段顺序。如db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )表示size等于{ h: 14, w: 21, uom: "cm" }的文档记录。 - 匹配的
field为数组类型,value为字符串类型,则为包含匹配。如db.inventory.find( { tags: "red" } )表示tags中包含red的文档记录 - 匹配的
field为数组类型,value也为数组类型,则为相等匹配。如db.inventory.find( { tags: [ "red", "blank" ] } )表示tags等于[ "red", "blank" ]的文档记录。
# 查询inventory集合中status等于D的文档记录 > db.inventory.find( { status: "D" } ); { "_id" : ObjectId("5e106095fa6c58a3116cee8d"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8e"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] } # 查询inventory集合中qty等于0的文档记录 > db.inventory.find( { qty: 0 } ); { "_id" : ObjectId("5e106095fa6c58a3116cee8e"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] } # 查询inventory集合中size等于文档{ h: 14, w: 21, uom: "cm" }的文档记录 > db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } ) { "_id" : ObjectId("5e106095fa6c58a3116cee8b"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] } # 查询inventory集合中tags数组中包含red的文档记录 > db.inventory.find( { tags: "red" } ) { "_id" : ObjectId("5e106095fa6c58a3116cee8b"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8c"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8d"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] } { "_id" : ObjectId("5e106095fa6c58a3116cee8e"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] } # inventory集合中tags为["red", "blank"]的文档记录 > db.inventory.find( { tags: [ "red", "blank" ] } ) { "_id" : ObjectId("5e106095fa6c58a3116cee8c"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] } - 匹配的
-
指定要返回的字段:使用
db.collection.find(<query document>,<projection document> )方法传入一个投影文档,可以指定返回的字段。在投影文档中:<field>: 1表示在文档返回中包含指定字段<field>: 0表示在文档返回中排除指定字段
返回文档中
_id是默认返回的# 返回inventory集合中所有文档的_id,item和status字段 > db.inventory.find( { }, { item: 1, status: 1 } ); { "_id" : ObjectId("5e106095fa6c58a3116cee8b"), "item" : "journal", "status" : "A" } { "_id" : ObjectId("5e106095fa6c58a3116cee8c"), "item" : "notebook", "status" : "A" } { "_id" : ObjectId("5e106095fa6c58a3116cee8d"), "item" : "paper", "status" : "D" } { "_id" : ObjectId("5e106095fa6c58a3116cee8e"), "item" : "planner", "status" : "D" } { "_id" : ObjectId("5e106095fa6c58a3116cee8f"), "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" }
原文地址:Getting Started
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




