1.核心存储
collection集合存储,存储未bson数据或者文档 2.集合创建相关操作 创建集合
db.createCollection(name, options)
更新集合名
db.adminCommand({
renameCollection: "sourceDb.sourceCollection",
to: "targetDb.targetCollection",
dropTarget: <boolean>
})
删除集合
db.collectionName.drop()
3。集合中数据插入操作相关
插入单个数据
db.myCollection.insertOne({
name: "Alice",
age: 25,
city: "New York"
});
插入多个数据
db.myCollection.insertMany([
{ name: "Bob", age: 30, city: "Los Angeles" },
{ name: "Charlie", age: 35, city: "Chicago" }
]);
替换数据
db.collection.save(document, options)//已弃用
db.collection.insertOne() //插入同_id字段默认替换
db.collection.replaceOne() //用该方法替换数据
插入单个或者多个文档
db.collection.insert(document_or_array)//已弃用
db.collection.InsertOne(document)
db.collection.InsertMany(document1,document2)
插入多个文档性能优化,增加orered表示即使顺序插入失败,也不影响其余文档插入
db.collection.insertMany([document1, document2, ...], { ordered: false })
插入文档时候,mongo会根据创建集合时添加的验证规则,进行验证,如果不通过,则插入失败
4.更新操作
更新单个数据
db.collection.updateOne(filter, update, options)
其中filter表示过滤条件,update表示需要更新的文档或者制定数据,options表示可操作 例如:
db.myCollection.updateOne(
{ name: "Alice" }, // 过滤条件
{ $set: { age: 26 } }, // 更新操作
{ upsert: false } // 可选参数
);
同时更新多个数据,筛选条件和options限制和更新单个数据一样
db.collection.updateMany(filter, update, options)
直接替换某个文档,用法和上面更新一样
db.collection.replaceOne(filter, replacementDoc, options)
带原来文档的替换方法,findOneAndUpdate,会返回一个更新前/后的方法,returnDocument:"after"或者“”before”进行制定
db.myCollection.findOneAndUpdate(
{ name: "Charlie" }, // 过滤条件
{ $set: { age: 36 } }, // 更新操作
{ returnDocument: "after" } // 可选参数,返回更新后的文档
);
5.删除操作相关
删除单个
db.collection.deleteOne(filter, options)
示例:db.myCollection.deleteOne({ name: "Alice" });
删除多个
db.collection.deleteMany(filter, options)
示例:db.myCollection.deleteMany({ status: "inactive" });
删除单个同时返回删除的文档:
db.collection.findOneAndDelete(filter, options)
删除操作的options可包含的选项:
findOneAndDelete
):指定返回的字段。findOneAndDelete
):指定排序顺序以确定要删除的文档。6.查询相关操作
用find或者findOne方法,以结构化方式来显示所有文档,findOne只匹配返回的第一个
db.collection.find(query, projection)
示例:
db.myCollection.find();//显示当前collection所有文档
db.myCollection.find({ age: { $gt: 25 } });//查询满足条件的文档
db.col.find().pretty()//增加pretty方法,格式化显示
db.collection.findOne(query, projection)//只查找单个,多个匹配只返回第一个
7.高级操作符:
$gt、$lt、$gte、$lte、$eq、$ne 大于,小于,大于等于,等于,不等于
8.限制方法,
limit()限制返回的文档数量
db.collection.find().limit(<limit>)
使用示例:
// 返回前 10 个文档
db.myCollection.find().limit(10);
skip()方法跳过指定数量的数据
db.collection.find().skip(<skip>)
示例:
db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
输出:{ "title" : "Java 教程" }
// 跳过前 10 个文档,返回接下来的 10 个文档
db.myCollection.find().skip(10).limit(10);
sort() 方法对数据进行排序
db.collection.find().sort({ field1: 1, field2: -1 })
使用示例:
// 先按 age 字段升序排序,再按 createdAt 字段降序排序
db.myCollection.find().sort({ age: 1, createdAt: -1 });
9.数据索引
createIndex() 方法来创建索引
使用示例:
// 创建唯一索引
db.collection.createIndex( { field: 1 }, { unique: true } )
// 创建后台运行的索引
db.collection.createIndex( { field: 1 }, { background: true } )
// 创建稀疏索引
db.collection.createIndex( { field: 1 }, { sparse: true } )
// 创建文本索引并指定权重
db.collection.createIndex( { field: "text" }, { weights: { field: 10 } } )
创建地理空间索引
对于存储地理位置数据的字段,可以使用 2dsphere 或 2d 索引类型来创建地理空间索引。
// 2dsphere 索引,适用于球形地理数据
db.collection.createIndex( { location: "2dsphere" } )
// 2d 索引,适用于平面地理数据
db.collection.createIndex( { location: "2d" } )
哈希索引:使用哈希索引对字段进行哈希,以支持大范围的数值查找。
db.collection.createIndex( { field: "hashed" } )
删除索引
// 删除指定的索引
db.collection.dropIndex( "indexName" )
// 删除所有索引
db.collection.dropIndexes()
10.聚合函数-用于统计计算数据
语法格式:db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
使用示例:
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
//集合计算每个作者所写的文章数,使用aggregate()计算结果如下
{
"result" : [
{
"_id" : "runoob.com",
"num_tutorial" : 2
},
{
"_id" : "Neo4j",
"num_tutorial" : 1
}
],
"ok" : 1
}
>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。