MongoDB学习第四篇 --- Query操作

一、shell执行mongodb查询(简单json数据结构)

查询所有:db.inventory.find()

按条件查询:db.inventory.find( { status: "D" } )

in条件查询:db.inventory.find( { status: { $in: [ "A", "D" ] } } )

and和范围条件查询:db.inventory.find( { status: "A", qty: { $lt: 30 } } )

or条件查询:db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )

and和or的复合条件:db.inventory.find( {status: "A",   $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]})

查询一条:db.collection.findOne()

二、python执行mongodb查询(简单json数据结构)

查询所有:cursor = db.inventory.find({})

条件查询:cursor = db.inventory.find({"status": "D"})

in条件查询:cursor = db.inventory.find({"status": {"$in": ["A", "D"]}})

and条件查询:cursor = db.inventory.find({"status": "A", "qty": {"$lt": 30}})

or条件查询:cursor = db.inventory.find({"$or": [{"status": "A"}, {"qty": {"$lt": 30}}]}

and和or的复合条件:cursor = db.inventory.find({"status": "A","$or": [{"qty": {"$lt": 30}}, {"item": {"$regex": "^p"}}]})

查询一条:pymongo.collection.Collection.find_one()

  

三、java执行mongodb查询(简单json数据结构)

查询所有:FindIterable<Document> findIterable = collection.find(new Document());

条件查询:findIterable = collection.find(eq("status", "D"));

in条件查询:findIterable = collection.find(in("status", "A", "D"));

and条件查询:findIterable = collection.find(and(eq("status", "A"), lt("qty", 30)));

or条件查询:findIterable = collection.find(or(eq("status", "A"), lt("qty", 30)));

and和or的复合条件:findIterable = collection.find(and(eq("status", "A"),or(lt("qty", 30), regex("item", "^p"))));

四、嵌套json数据格式的查询:

1.shell方式:

如果数据格式如下:

db.inventory.insertMany( [
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
那么,shell如下用.的方式进行json数据获取,也可以进行各种条件的查询。
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )能查到
db.inventory.find(  { size: { w: 21, h: 14, uom: "cm" } }  )查不到 (json中的数据一体化,位置不可移动)
db.inventory.find( { "size.uom": "in" } )     # 执行条件的查询
db.inventory.find( { "size.h": { $lt: 15 } } )  # 范围条件的查询
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } ) #and条件的查询

2.python方式:

如果数据格式如下:

from bson.son import SON
db.inventory.insert_many([
    {"item": "journal",
     "qty": 25,
     "size": SON([("h", 14), ("w", 21), ("uom", "cm")]),
     "status": "A"},
    {"item": "notebook",
     "qty": 50,
     "size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
     "status": "A"},
    {"item": "paper",
     "qty": 100,
     "size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
     "status": "D"},
    {"item": "planner",
     "qty": 75,
     "size": SON([("h", 22.85), ("w", 30), ("uom", "cm")]),
     "status": "D"},
    {"item": "postcard",
     "qty": 45,
     "size": SON([("h", 10), ("w", 15.25), ("uom", "cm")]),
     "status": "A"}])
python嵌入式查询语句如下:
cursor = db.inventory.find({"size": SON([("h", 14), ("w", 21), ("uom", "cm")])})
cursor = db.inventory.find({"size": SON([("w", 21), ("h", 14), ("uom", "cm")])})
cursor = db.inventory.find({"size.uom": "in"})
cursor = db.inventory.find({"size.h": {"$lt": 15}})
cursor = db.inventory.find({"size.h": {"$lt": 15}, "size.uom": "in", "status": "D"})


五、嵌套array数据格式的查询:

1.shell查询array嵌套数据如下:

db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);

shell查询语句如下:

db.inventory.find( { tags: ["red", "blank"] } ) # 查询array固定内容的数据
db.inventory.find( { tags: { $all: ["red", "blank"] } } ) # 数据含有  red和blank同时有  的数据
db.inventory.find( { tags: "red" } ) # 数据含有  red  的数据  
db.inventory.find( { dim_cm: { $gt: 25 } } )  # 数据含有  数据大于25 的数据
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } ) # 数据含有  其中一个数据大于15 另一个数据小于20 的数据
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } ) # 数据含有 可以同时满足 大于22并且小于30 的数据
db.inventory.find( { "dim_cm.1": { $gt: 25 } } ) # 查询dim_cm中的第二个元素大于25 的数据
db.inventory.find( { "tags": { $size: 3 } } )  # 查询tags array中的元素为3个 的数据

python查询语句如下:

cursor = db.inventory.find({"tags": ["red", "blank"]})
cursor = db.inventory.find({"tags": {"$all": ["red", "blank"]}})
cursor = db.inventory.find({"tags": "red"})
cursor = db.inventory.find({"dim_cm": {"$gt": 25}})
cursor = db.inventory.find({"dim_cm": {"$gt": 15, "$lt": 20}})
cursor = db.inventory.find({"dim_cm": {"$elemMatch": {"$gt": 22, "$lt": 30}}})
cursor = db.inventory.find({"dim_cm.1": {"$gt": 25}})
cursor = db.inventory.find({"tags": {"$size": 3}})


六、同时嵌套array和json数据格式的查询:
如果数据如下:
db.inventory.insertMany( [
   { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
   { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
   { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
   { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);

shell查询语句如下:

db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } ) # 查询含有 instock array中的json元素为{ warehouse: "A", qty: 5 } 的数据
db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } ) # 查询含有 instock array中的json元素为{qty: 5, warehouse: "A" } 的数据查不到 (json中的数据一体化,位置不可移动)
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } ) # 查询含有 instock中的第一个json元素中的qty字段小于或等于 20 的数据
db.inventory.find( { 'instock.qty': { $lte: 20 } } )  # 查询含有 instock中的任意json元素中的qty小于或等于 20 的数据
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )  # 查询含有  instock中的json元素 同时满足qty为5,warehouse为A  的数据
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )  # 查询含有 instock中的json元素 同时满足qty大于10并小于等于20 的数据
# 如果不用 $elemMatch: 函数
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )# 查询含有 instock中的qty为5 或者 instock中的warehousr为A 的数据
db.inventory.find( { "instock.qty": { $gt: 10,  $lte: 20 } } ) # 查询含有 instock中的qty 大于10 或 小于等于20 的数据

  

七、筛选查询结果的字段:

如果数据结构为:
db.inventory.insertMany( [
  { item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
  { item: "notebook", status: "A",  size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
  { item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
  { item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
  { item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
查询语句和字段筛选的操作为:
db.inventory.find( { status: "A" } )  # 管道1:查询status为A的数据 
db.inventory.find( { status: "A" }, { item: 1, status: 1 } ) # 管道1:查询status为A的数据;   管道2:只显示item和status字段 (和_id字段 默认显示)
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } ) # 管道1:查询status为A的数据;   管道2:只显示item和status字段 (隐藏_id字段)
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } ) # 管道1:查询status为A的数据;   管道2:除去status和instock 其他都显示
db.inventory.find({ status: "A" }, { item: 1, status: 1, "size.uom": 1 } ) # 管道1:查询status为A的数据;   管道2:只显示item和status和size中的uom字段
db.inventory.find({ status: "A" },{ "size.uom": 0 }) # 管道1:查询status为A的数据;   管道2: 除去size中的uom字段 其他都显示
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )  # 管道1:查询status为A的数据;   管道2:只显示 item和status和instock中的qty字段
db.inventory.find( { status: "A" }, { name: 1, status: 1, instock: { $slice: -1 } } ) # 管道1:查询status为A的数据;   管道2:只显示name和status和instock中的倒数第一个元素
八、字段不存在和null字段的不同之处:
数据为:
db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])
查询语句为:
db.inventory.find( { item: null } ) # 查询所有 item不存在或为null 的数据
db.inventory.find( { item : { $type: 10 } } )  # 查询所有  item存在 但是为null 的数据
db.inventory.find( { item : { $exists: false } } )  # 查询所有 不存在item 的数据



原文地址:https://www.cnblogs.com/malcolmfeng/p/6924562.html