mongo库如何查询(mongo简单查询联合查询与删除语法)(1)

简单查询

举例:

查询2017年以前的数据:

db.xxCollection.find({"uploadDate":{$lt:new Date(2017,1,1)}});

(>) 大于 - $gt

(<) 小于 - $lt

(>=) 大于等于 - $gte

(<= ) 小于等于 - $lte

$in

db.xxCollection.find( { status: { $in: [ "A", "D" ] } } )

相当于sql的in

$or

db.xxCollection.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )

$and也有,不过什么都不加就是and的意思,所以一般用不到

排序:

db.xxCollection.find({"uploadDate":{$lte:new Date(2017,1,1)}).sort({"uploadDate":-1});

表示按uploadDate降序排列

限定返回值:

db.fs.files.find({"uploadDate":{$lte:new Date(2017,1,1)}},{"filename":1}).sort({"uploadDate":-1});

这样会返回filename。1表示返回,0是不返回

但是_id是默认返回的,所以这条语句会返回两个字段

db.fs.files.find({"uploadDate":{$lte:new Date(2017,1,1)}},{"filename":1,"_id":0}).sort({"uploadDate":-1});

如果,特地指定哪个不返回,那么就是返回其他所有的

db.fs.files.find({"uploadDate":{$lte:new Date(2017,1,1)}},{"_id":0}).sort({"uploadDate":-1});

会返回除_id外的所有字段

关联查询

这个语法有点麻烦

需要用到聚合函数中的 $lookup

$lookup的语法

{ $lookup: { from: <要关联的集合>, localField: <当前的集合的字段>, foreignField: <要关联的集合的字段>, as: <返回的字段名> } }

举例:

订单表:

db.orders.insert([ { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 }, { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }, { "_id" : 3 } ])

商品表:

db.inventory.insert([ { "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 }, { "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 }, { "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 }, { "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 }, { "_id" : 5, "sku": null, description: "Incomplete" }, { "_id" : 6 } ])

根据订单的item关联查询出商品信息:

db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } } ])

相当于sql中:

SELECT *, inventory_docs FROM orders WHERE inventory_docs IN (SELECT * FROM inventory WHERE sku= orders.item);

返回的结果:

{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2, "inventory_docs" : [ { "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 } ] } { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1, "inventory_docs" : [ { "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 } ] } { "_id" : 3, "inventory_docs" : [ { "_id" : 5, "sku" : null, "description" : "Incomplete" }, { "_id" : 6 } ] }

增加查询条件:$match

查询订单金额大于10的

db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } }, { $match: { "price": {$gt:10} } } ])

$match执行的比较,是基于lookup的结果来的

将"price": {$gt:10}换成“inventory_docs.instock": {$gt:100}

查询库存大于100的

指定返回字段$project

{ $project:{ "inventory_docs": 1, "item": 1, "_id": 0 } }

汇总起来:

db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } }, { $match: { "inventory_docs.instock": {$gt:100} } }, { $project:{ "inventory_docs": 1, "item": 1, "_id": 0 } } ])

返回:

{ "item" : "almonds", "inventory_docs" : [ { "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 } ] }

删除

每次在数据库上做删除操作,我都很紧张。就怕搞错了条件语句。

db.xxCollection.deleteOne({条件})

db.xxCollection.deleteMany({条件})

db.collection.findOneAndDelete({条件},{排序})

可以加排序字段,用来限制删除哪个元素,如

db.scores.findOneAndDelete( { "name" : "A. MacDyver" }, { sort : { "points" : 1 } } )

提醒:

1,任何时候delete语句都要有条件

2,删除前,先用find语句查一查,不要怕麻烦

关于mongo删除操作的几点说明:

1,删除documents不会删除索引,即使是删了所有的documents

2,原子性。所有的写操作在mongo中都是原子性的

,