1、db.collection.updateMany() 修改集合中的多个文档。
db.getCollection('user').find({
"pId":"3332a512df604a74a72f267cf246"}).updateMany({
"pId":"c8018dd802a644a19517790336f"})
2、模糊查询
db.getCollection('user').find({
name:{
$regex:"AA"}})
db.getCollection('user').find({
"name":{
$regex:/AA/ }})
db.getCollection('user').find({
name:/AA/})
3、查询name是否为AA,BB,CC,DD的记录
db.getCollection('user').find({
"name":{
$in:["AA","BB","CC","DD"]}}) //属于 - in
db.getCollection('user').find({
"name":{
$nin:["AA","BB","CC","DD"]}}) //不属于 - nin
4、按照时间排序
db.getCollection('user').find({
name:{
$regex:"AA"}}).sort({
lastUpdatedTime : 1 }) //时间正序
db.getCollection('user').find({
name:{
$regex:"AA"}}).sort({
lastUpdatedTime : -1 }) //时间倒序
5、字段是否存在
db.getCollection('user').find({
age:{
$exists:true}})
6、对数组中的某一个元素进行查询
db.getCollection('template').find({
"content.pages.questions.type":"A"})
7、limit() 和skip() 方法操作
使用limit() 方法来读取指定数量的数据,limit方法接受一个数字参数作为读取的记录条数
使用skip() 方法来跳过指定数量的数据,skip方法接受一个数字参数作为跳过的记录条数
db.getCollection('user').find({
}).limit(5).skip(1) — 跳过第1条,展示第2条到第6条
8、时间范围查询
greater than(大于)
less than(小于)
(>) 大于 - $gt
(<) 小于 - $lt
(>=) 大于等于 - $gte
(<= ) 小于等于 - $lte
db.getCollection('student').find({
"createdTime":{
$lt:new Date(2019,8,16)}}) //创建时间在2019.8.16之前的记录
db.getCollection('student').find({
"createdTime":{
$lte:new Date(2019,8,31),$gte:new Date(2019,3,1)}}) //创建时间在2019.3.1到2019.8.31之间的记录
9、更新语句,如果你要修改多条相同的文档,则需要设置 multi 参数为 true
db.getCollection('user').update({
"name":"张三"},{
$set:{
"name":"李四"}},{
multi:true})
10、具体查询文档中某个字段内包含的具体值
db.getCollection('user').find({
"content.studentId.username" : "AA"})