tiancheng91 / collection

笔记
https://github.com/tiancheng91/collection/issues
21 stars 1 forks source link

mongo手册 #11

Open tiancheng91 opened 6 years ago

tiancheng91 commented 6 years ago

数据集和集合

文档

crud

http://www.mongoing.com/docs/core/crud.html

复制集

查询

db.users.find( { status: { $in: [ "P", "D" ] } } )
db.users.find( { status: "A", age: { $lt: 30 } } )
// or
db.users.find(
   {
     $or: [ { status: "A" }, { age: { $lt: 30 } } ]
   }
)
// and(or)
db.users.find(
   {
     status: "A",
     $or: [ { age: { $lt: 30 } }, { type: 1 } ]
   }
)
// indoc query
db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } )    // artist, food 有顺序
// 
db.users.find( { "favorites.artist": "Picasso" } )

// 数组精确匹配
db.users.find( { badges: [ "blue", "black" ] } )
// 匹配一个数组元素
db.users.find( { badges: "black" } )
// 匹配指定元素
db.users.find( { "badges.0": "black" } )

// $gt, $lt: 命中字段为数组时,比较数组大小,否则比较val值
// 数组查询中, 未指定下标时, 数组中任意一个满足就返回
db.users.find( { finished: { $gt: 15, $lt: 20 } } ) // 数组大小满足 
db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )  // 数组元素满足

// 
db.users.find( { 'points.0.points': { $lte: 55 } } ) // 指定索引
db.users.find( { 'points.points': { $lte: 55 } } )   // 任意一项满足
db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } )  // 多个条件
db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } )

映射(projection)字段, 返回部分doc

db.users.find( { status: "A" }, { name: 1, status: 1 } )        // 包含
db.users.find( { status: "A" }, { favorites: 0, points: 0 } )   // 排除
db.users.find(
   { status: "A" },
   { name: 1, status: 1, "favorites.food": 1 }      // `favorites.food` 路径可以表示 hash路径或不包含下标数组
)
db.users.find( { status: "A" }, { name: 1, status: 1, points: { $slice: -1 } } ) // 返回数组部分内容

null处理

db.users.find( { name: null } )
返回 (非稀疏索引时,稀疏索引只会为存在该字段的文档建索引)
{ "_id" : 900, "name" : null }
{ "_id" : 901 }

// 按字段类型查询
db.users.find( { name : { $type: 10 }       // type值查询文档
// 返回不包含字段的文档
db.users.find( { name : { $exists: false } } )

更新

db.users.updateOne(
   { "favorites.artist": "Picasso" },
   {
     $set: { "favorites.food": "pie", type: 3 },
     $currentDate: { lastModified: true }       // $currentDate 方法更新当前日期到指定字段
   }
)

db.users.update({
    {}, {}, { multi: true }         // updateOne, updateMany, update 默认更新单文档
})

// 支持更多特性
db.collection.findOneAndUpdate(
   <filter>,
   <update>,
   {
     projection: <document>,    // 需要返回的字段   
     sort: <document>,      // 查询顺序
     maxTimeMS: <number>,   // 多长时间内结束   
     upsert: <boolean>,     // 不存在时创建 
     returnNewDocument: <boolean>,  // 返回新插入的文档
     collation: <document>
   }
)

// db.collection.findAndModify({});     // 支持各种增删改操作

删除

db.collection.deleteOne()
db.collection.remove()

bulkWrite 选项,批量写入有性能问题时可以考虑

聚合

文本索引

一个集合只能有一个文本索引,但允许有多个字段, 另外view中不支持文本索引 db.stores.createIndex( { name: "text", description: "text" } )

查询, 或关系 db.stores.find( { $text: { $search: "java coffee shop" } } )

相关度

db.stores.find(
   { $text: { $search: "coffee shop cake" } },
   { score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )

中文支持, 需要Rosette扩展, 并购买对应语言(RLP许可 建议使用第三方服务, 如 opensearch

数据模型

tiancheng91 commented 2 years ago
db.createCollection("files", {
    storageEngine: { wiredTiger: { configString: "block_compressor=zstd,type=lsm" } } 
})