mongodb入门
导航
登录
mongo --host <ip> --port <端口>
数据导入导出
mongoexport --host 127.0.0.1 --port 27110 -d dbName -c collName -o file.dat
mongoimport --host 127.0.0.1 --port 27110 -d dbName -c collName file.dat
模糊查询
db.getCollection('alert').find({"alert.identifier":/^21030/})
插入数据
db.inventory.insertOne({ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } })
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
查询数据
查询条件:{ <field1>: <value1>, ... }
查询一条记录:db.collection.findOne( )
查询全部:db.inventory.find( {} )
按条件查询:
db.inventory.find( { status: "D" } ) 【SELECT * FROM inventory WHERE status = "D"】
IN查询:
db.inventory.find( { status: { $in: [ "A", "D" ] } } ) 【SELECT * FROM inventory WHERE status in ("A", "D")】
特殊条件AND:
db.inventory.find( { status: "A", qty: { $lt: 30 } } ) 【SELECT * FROM inventory WHERE status = "A" AND qty < 30】
特殊条件OR
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } ) 【SELECT * FROM inventory WHERE status = "A" OR qty < 30】
联合条件
db.inventory.find( { status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] } ) 【SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")】
更新数据
db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <replacement>, <options>)
语法
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
更新一条记录某字段。使用$set
来更新指定字段信息。使用$currentDate
将最后修改时间变成当前时间。如果最后修改时间字段lastModified
不存在将会自动创建该字段。
db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
更新多条记录某字段。
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)
替换一条记录。db.collection.replaceOne()
{ "_id" : ObjectId("59c3b1145ac95ea59ae660fb"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
替换成
{ "_id" : ObjectId("59c3b1145ac95ea59ae660fb"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 40 } ] }
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
删除数据
删除多条:
db.inventory.deleteMany({ status : "A" })
删除一条:
db.inventory.deleteOne( { status: "D" } )
文本搜索
db.stores.insert(
[
{ _id: 1, name: "Java Hut", description: "Coffee and cakes" },
{ _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
{ _id: 3, name: "Coffee Shop", description: "Just coffee" },
{ _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
{ _id: 5, name: "Java Shopping", description: "Indonesian goods" }
]
)
创建索引
db.stores.createIndex( { name: "text", description: "text" } )
使用$text Operator
查询包含java,coffee,shop的
db.stores.find( { $text: { $search: "java coffee shop" } } )
查询包含java或者coffee shop的
db.stores.find( { $text: { $search: "java \"coffee shop\"" } } )
查询包含java或者shop但是不包含coffee的
db.stores.find( { $text: { $search: "java shop -coffee" } } )
排序
db.stores.find(
{ $text: { $search: "java coffee shop" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )