文章目录
1、新增文档 1.1、进入容器 1.2、向集合插入一个新文档 1.3、查询文档 1.4、向集合插入一个或者多个文档
2、查询文档 2.1、查询年龄等于20 2.2、查询年龄是20的男性 2.3、查询年龄是20或者性别是男 2.4、查询年龄小于23 2.5、查询年龄小于等于23 2.6、查询大于等于23 2.7、查询年龄不等于23 2.8、模糊查询包含 小 的名字 2.9、模糊查询包含 小 的名字 以及 升序 排序 2.10、模糊查询包含 小 的名字 以及 降序 排序 2.11、跳过前两个匹配的文档,然后返回接下来的两个文档
3、更新文档 3.1、更新({name:'小羊'},{$set:{age:30,gender:'女'}})匹配的文档 3.2、更新({age:23},{$set:{age:30}})匹配的文档 3.3、更新({age:30},{$set:{age:23}})匹配的文档 3.4、更新({name:/小/},{$set:{age:22}})匹配的文档
4、删除文档 4.1、删除age等于21的文档 4.2、查找第一个age字段值为22的文档,并将其删除 4.3、查找所有age字段值为22的文档,并将它们全部删除
1、新增文档
1.1、进入容器
[ root@localhost docker] # docker exec - it mongodb mongosh
Current Mongosh Log ID: 66 a8ce38d0b85e052bb78f05
Connecting to: mongodb:
Using MongoDB: 7.0 .0
Using Mongosh: 1.10 .6
For mongosh info see: https:
-- -- --
The server generated these startup warnings when booting
2024 - 07 - 30 T10: 58 : 29.512 + 00 : 00 : Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024 - 07 - 30 T10: 58 : 29.513 + 00 : 00 : / sys/ kernel/ mm/ transparent_hugepage/ enabled is 'always' . We suggest setting it to 'never'
2024 - 07 - 30 T10: 58 : 29.513 + 00 : 00 : / sys/ kernel/ mm/ transparent_hugepage/ defrag is 'always' . We suggest setting it to 'never'
2024 - 07 - 30 T10: 58 : 29.513 + 00 : 00 : vm. max_map_count is too low
-- -- --
test>
1.2、向集合插入一个新文档
atguigu> db. myCollection. insertOne ( { name: '小蓝' , age: 21 } )
{
acknowledged: true,
insertedId: ObjectId ( "66a9b0067066bd635ba48cf0" )
}
1.3、查询文档
atguigu> db. myCollection. find ( )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 }
]
1.4、向集合插入一个或者多个文档
atguigu> db. myCollection. insertMany ( [ { name: '小狗' , age: 22 , gender: '女' } , { name: '小猫' , age: 23 , gender: '男' } ] )
{
acknowledged: true,
insertedIds: {
'0' : ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
'1' : ObjectId ( "66a9b2597066bd635ba48cf2" )
}
}
atguigu> db. myCollection. find ( )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 } ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf2" ) ,
name: '小猫' ,
age: 23 ,
gender: '男'
}
]
2、查询文档
2.1、查询年龄等于20
atguigu> db. myCollection. find ( { age: 20 } )
[ { _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ]
atguigu> db. myCollection. insertMany ( [ { name: '小牛' , age: 23 , gender: '男' } , { name: '小羊' , age: 20 , gender: '男' } ] )
{
acknowledged: true,
insertedIds: {
'0' : ObjectId ( "66a9cf7d7066bd635ba48cf3" ) ,
'1' : ObjectId ( "66a9cf7d7066bd635ba48cf4" )
}
}
atguigu> db. myCollection. find ( { age: 20 } )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
2.2、查询年龄是20的男性
atguigu> db. myCollection. find ( { age: 20 , gender: '男' } )
[
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
2.3、查询年龄是20或者性别是男
atguigu> db. myCollection. find ( { $or: [ { age: 20 } , { gender: '男' } ] } )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf2" ) ,
name: '小猫' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf3" ) ,
name: '小牛' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
2.4、查询年龄小于23
atguigu> db. myCollection. find ( { age: { $lt: 23 } } )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 } ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
2.5、查询年龄小于等于23
atguigu> db. myCollection. find ( { age: { $lte: 23 } } )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 } ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf2" ) ,
name: '小猫' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf3" ) ,
name: '小牛' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
2.6、查询大于等于23
atguigu> db. myCollection. find ( { age: { $gte: 23 } } )
[
{
_id: ObjectId ( "66a9b2597066bd635ba48cf2" ) ,
name: '小猫' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf3" ) ,
name: '小牛' ,
age: 23 ,
gender: '男'
}
]
2.7、查询年龄不等于23
atguigu> db. myCollection. find ( { age: { $ne: 23 } } )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 } ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
2.8、模糊查询包含 小 的名字
atguigu> db. myCollection. find ( { name: { $regex: '小' } } )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 } ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf2" ) ,
name: '小猫' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf3" ) ,
name: '小牛' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
atguigu> db. myCollection. find ( { name: / 小/ } )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 } ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf2" ) ,
name: '小猫' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf3" ) ,
name: '小牛' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
2.9、模糊查询包含 小 的名字 以及 升序 排序
atguigu> db. myCollection. find ( { name: / 小/ } ) . sort ( { age: 1 } )
[
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
} ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 } ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf2" ) ,
name: '小猫' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf3" ) ,
name: '小牛' ,
age: 23 ,
gender: '男'
}
]
2.10、模糊查询包含 小 的名字 以及 降序 排序
atguigu> db. myCollection. find ( { name: / 小/ } ) . sort ( { age: - 1 } )
[
{
_id: ObjectId ( "66a9b2597066bd635ba48cf2" ) ,
name: '小猫' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf3" ) ,
name: '小牛' ,
age: 23 ,
gender: '男'
} ,
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 } ,
{ _id: ObjectId ( "66a99e507066bd635ba48cef" ) , name: '小明' , age: 20 } ,
{
_id: ObjectId ( "66a9cf7d7066bd635ba48cf4" ) ,
name: '小羊' ,
age: 20 ,
gender: '男'
}
]
2.11、跳过前两个匹配的文档,然后返回接下来的两个文档
atguigu> db. myCollection. find ( { name: / 小/ } ) . sort ( { age: - 1 } ) . limit ( 2 ) . skip ( 2 )
[
{
_id: ObjectId ( "66a9b2597066bd635ba48cf1" ) ,
name: '小狗' ,
age: 22 ,
gender: '女'
} ,
{ _id: ObjectId ( "66a9b0067066bd635ba48cf0" ) , name: '小蓝' , age: 21 }
]
3、更新文档
3.1、更新({name:‘小羊’},{$set:{age:30,gender:‘女’}})匹配的文档
atguigu> db. myCollection. update ( { name: '小羊' } , { $set: { age: 30 , gender: '女' } } )
DeprecationWarning: Collection. update ( ) is deprecated. Use updateOne, updateMany, or bulkWrite.
{
acknowledged: true,
insertedId: null,
matchedCount: 1 ,
modifiedCount: 1 ,
upsertedCount: 0
}
3.2、更新({age:23},{$set:{age:30}})匹配的文档
atguigu> db. myCollection. updateOne ( { age: 23 } , { $set: { age: 30 } } )
{
acknowledged: true,
insertedId: null,
matchedCount: 1 ,
modifiedCount: 1 ,
upsertedCount: 0
}
3.3、更新({age:30},{$set:{age:23}})匹配的文档
atguigu> db. myCollection. updateMany ( { age: 30 } , { $set: { age: 23 } } )
{
acknowledged: true,
insertedId: null,
matchedCount: 3 ,
modifiedCount: 3 ,
upsertedCount: 0
}
3.4、更新({name:/小/},{$set:{age:22}})匹配的文档
atguigu> db. myCollection. updateMany ( { name: / 小/ } , { $set: { age: 22 } } )
{
acknowledged: true,
insertedId: null,
matchedCount: 6 ,
modifiedCount: 5 ,
upsertedCount: 0
}
4、删除文档
4.1、删除age等于21的文档
atguigu> db. myCollection. remove ( { age: 21 } )
DeprecationWarning: Collection. remove ( ) is deprecated. Use deleteOne, deleteMany, findOneAndDelete, or bulkWrite.
{ acknowledged: true, deletedCount: 0 }
4.2、查找第一个age字段值为22的文档,并将其删除
atguigu> db. myCollection. deleteOne ( { age: 22 } )
{ acknowledged: true, deletedCount: 1 }
4.3、查找所有age字段值为22的文档,并将它们全部删除
atguigu> db. myCollection. deleteMany ( { age: 22 } )
{ acknowledged: true, deletedCount: 5 }