内容太多了,感觉不好写,就写点入门的吧,其他参考 参考_MonogDB 中文网 (mongodb.net.cn) 虽然内容是机器翻译的,但也还好,基本能看. 相关概念: database 数据库 collection 集合,相当于数据库表 document 文档,相当于数据记录行
docker run -d --name mongo -p 27017:27017 mongo
docker exec -it mongo bash
mongosh
查看当前数据库
db
查看数据库列表
show dbs
查看数据集列表
show tables
show collections
选择数据库: 数据库不存在也可以,而当插入数据后,数据库就会自动创建
use <database>
删除当前数据库
db.dropDatabase()
创建集合或视图
db.createCollection(
<name>,
{
capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document>,
viewOn: <string>,
pipeline: <pipeline>,
collation: <document>,
writeConcern: <document>
}
)
删除集合
db.collection.drop()
创建视图
db.createView(
"<viewName>",
"<source>",
[<pipeline>],
{
"collation" : { <collation> }
}
)
db.createCollection(
"<viewName>",
{
"viewOn" : "<source>",
"pipeline" : [<pipeline>],
"collation" : { <collation> }
}
)
插入文档
db.collection.insert(
<document or array of documents>,
{
writeConcern: <document>,
ordered: true //是否按顺序写入
}
)
db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)
db.collection.insertMany(
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
查找文档
db.collection.find(
query, //查询条件
projection //返回的字段 { name:1, _id:0 } ,表示取 name 字段, 不要默认包含的 _id 字段
)
db.collection.findOne(
query,
projection
)
更新文档
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>, // 是否自动创建
multi: <boolean>, // 是否更新多条
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // 手动指定索引
}
)
db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string>
}
)
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string>
}
)
删除文档
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>,
collation: <document>
}
)
db.collection.deleteOne(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)
db.collection.deleteMany(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)
构造查询
创建固定集合 products,整个集合空间大小 6142800B, 文档最大个数为 10000 个
db.createCollection('products', {capped:true, autoIndexId:true, size:6142800, max:10000 })
插入文档,直接插入文档,也会自动创建集合的
db.products.insert(
[
{ item: "pen", qty: 20 },
{ item: "eraser", qty: 25 },
{ item: "pencil", qty: 50, type: "no.2", _id: 11 }
]
)
查询条件 : where qty > 10 and qty <= 50 and item = "eraser" and (qty >= 0 or item <> "pencil" ) limit 0, 5
db.products.find({
qty: {$gt : 20, $lte : 50},
item:"eraser",
$or:[
qty: {$gte : 0},
item: {$ne:"pencil"}
]
}).limit(5).skip(0).pretty()
更新
db.col.update({item:'pen'}, {$set:{'qty':300}}, {multi:true})
php 示例
<?php
// 查询 ==================================================
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name' => '菜鸟教程', 'url' => 'http://www.runoob.com']);
$bulk->insert(['x' => 2, 'name' => 'Google', 'url' => 'http://www.google.com']);
$bulk->insert(['x' => 3, 'name' => 'taobao', 'url' => 'http://www.taobao.com']);
$manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
$filter = ['x' => ['$gt' => 1]];
$options = [
'projection' => ['_id' => 0], // 字段
'sort' => ['x' => -1], // 排序
];
// 查询数据
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('test.sites', $query);
foreach($cursor as $document) {
print_r($document);
}
// 更新 ============================================
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['x' => 2],
['$set' => ['name' => '菜鸟工具', 'url' => 'tool.runoob.com']],
['multi' => false, 'upsert' => false]
);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
// 删除 ===========================================================================
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['x' => 1], ['limit' => 1]); // limit 为 1 时,删除第一条匹配数据
$bulk->delete(['x' => 2], ['limit' => 0]); // limit 为 0 时,删除所有匹配数据
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
标签:示例,mongodb,db,collection,writeConcern,bulk,MongoDB,collation,php From: https://www.cnblogs.com/zbseoag/p/17552323.html