做防水广告在哪个网站最好邢台市建设工程质量监督网站
news/
2025/9/28 17:38:59/
文章来源:
做防水广告在哪个网站最好,邢台市建设工程质量监督网站,二手交易网网站建设目标,织梦网站上传的文章只显示摘要不显示内容如何修改r e p l a c e W i t h ‘ 可以将输入文档替换为指定的文档。该操作可以替换输入文档的所有字段#xff0c;包括 ‘ i d ‘ 字段。使用 ‘ replaceWith可以将输入文档替换为指定的文档。该操作可以替换输入文档的所有字段#xff0c;包括_id字段。使用 replaceWith‘可以将输… r e p l a c e W i t h ‘ 可以将输入文档替换为指定的文档。该操作可以替换输入文档的所有字段包括 ‘ i d ‘ 字段。使用 ‘ replaceWith可以将输入文档替换为指定的文档。该操作可以替换输入文档的所有字段包括_id字段。使用 replaceWith‘可以将输入文档替换为指定的文档。该操作可以替换输入文档的所有字段包括‘id‘字段。使用‘replaceWith还可以将内嵌文档提升到最顶级也可以把它替换掉。替换文档可以是任何能够解析为文档的有效表达式。 r e p l a c e W i t h ‘ 与 ‘ replaceWith与 replaceWith‘与‘replaceRoot有很多相似点但也有一些不同点。
语法
{ $replaceWith: replacementDocument }使用
如果replacementDocument不是一个文档或者被解析为一个错误的文档如文档不存在都会失败例如使用下面的文档创建一个集合
db.collection.insertMany([{ _id: 1, name : { first : John, last : Backus } },{ _id: 2, name : { first : John, last : McCarthy } },{ _id: 3, name: { first : Grace, last : Hopper } },{ _id: 4, firstname: Ole-Johan, lastname : Dahl },
])下面的$replaceWith会操作失败因为最后一个文档缺少name字段
db.collection.aggregate([{ $replaceWith: $name }
])要避免上面的错误可以使用$mergeObjects把name文档与某个缺省文档合并如
db.collection.aggregate([{ $replaceWith: { $mergeObjects: [ { _id: $_id, first: , last: }, $name ] } }
])也可以使用$match阶段在$replaceWith之前筛选掉name字段异常的数据
db.collection.aggregate([{ $match: { name : { $exists: true, $not: { $type: array }, $type: object } } },{ $replaceWith: $name }
])亦或者使用$ifNull表达式来指定其它的文档如
db.collection.aggregate([{ $replaceWith: { $ifNull: [ $name, { _id: $_id, missingName: true} ] } }
])举例
替换内嵌文档字段
使用下面的语句创建一个people集合
db.people.insertMany([{ _id : 1, name : Arlene, age : 34, pets : { dogs : 2, cats : 1 } },{ _id : 2, name : Sam, age : 41, pets : { cats : 1, fish : 3 } },{ _id : 3, name : Maria, age : 25 }
])下面的操作对people集合中的内嵌字段pets合并后进行替换结果为
{ dogs : 2, cats : 1, birds : 0, fish : 0 }
{ dogs : 0, cats : 1, birds : 0, fish : 3 }
{ dogs : 0, cats : 0, birds : 0, fish : 0 }使用嵌套的数组中文档替换
student集合有下面内容
db.students.insertMany([{_id : 1,grades : [{ test: 1, grade : 80, mean : 75, std : 6 },{ test: 2, grade : 85, mean : 90, std : 4 },{ test: 3, grade : 95, mean : 85, std : 6 }]},{_id : 2,grades : [{ test: 1, grade : 90, mean : 75, std : 6 },{ test: 2, grade : 87, mean : 90, std : 3 },{ test: 3, grade : 91, mean : 85, std : 4 }]}
])下面的操作会将grade字段大于或等于90的内嵌文档提升到顶层
db.students.aggregate( [{ $unwind: $grades },{ $match: { grades.grade : { $gte: 90 } } },{ $replaceWith: $grades }
] )结果
{ test : 3, grade : 95, mean : 85, std : 6 }
{ test : 1, grade : 90, mean : 75, std : 6 }
{ test : 3, grade : 91, mean : 85, std : 4 }使用新创建的文档替换
例1
集合sales包含以下文档
db.sales.insertMany([{ _id : 1, item : butter, price : 10, quantity: 2, date: ISODate(2019-03-01T08:00:00Z), status: C },{ _id : 2, item : cream, price : 20, quantity: 1, date: ISODate(2019-03-01T09:00:00Z), status: A },{ _id : 3, item : jam, price : 5, quantity: 10, date: ISODate(2019-03-15T09:00:00Z), status: C },{ _id : 4, item : muffins, price : 5, quantity: 10, date: ISODate(2019-03-15T09:00:00Z), status: C }
])假设为了要计算已完成销售的总金额即查找所有状态为C的销售记录使用$replaceWith阶段创建新文档计算总金额并使用变量NOW获取当前时间
db.sales.aggregate([{ $match: { status: C } },{ $replaceWith: { _id: $_id, item: $item, amount: { $multiply: [ $price, $quantity]}, status: Complete, asofDate: $$NOW } }
])操作返回以下结果
{ _id : 1, item : butter, amount : 20, status : Complete, asofDate : ISODate(2019-06-03T22:47:54.812Z) }
{ _id : 3, item : jam, amount : 50, status : Complete, asofDate : ISODate(2019-06-03T22:47:54.812Z) }
{ _id : 4, item : muffins, amount : 50, status : Complete, asofDate : ISODate(2019-06-03T22:47:54.812Z) }例2
reportedsales集合按季度和地区填入报销售额信息如下
db.reportedsales.insertMany( [{ _id: 1, quarter: 2019Q1, region: A, qty: 400 },{ _id: 2, quarter: 2019Q1, region: B, qty: 550 },{ _id: 3, quarter: 2019Q1, region: C, qty: 1000 },{ _id: 4, quarter: 2019Q2, region: A, qty: 660 },{ _id: 5, quarter: 2019Q2, region: B, qty: 500 },{ _id: 6, quarter: 2019Q2, region: C, qty: 1200 }
] )假设出于报告目的想查看按季度报告的销售数据例如
{ _id : 2019Q1, A : 400, B : 550, C : 1000 }可以使用下面的聚合管道
db.reportedsales.aggregate( [{ $addFields: { obj: { k: $region, v: $qty } } },{ $group: { _id: $quarter, items: { $push: $obj } } },{ $project: { items2: { $concatArrays: [ [ { k: _id, v: $_id } ], $items ] } } },{ $replaceWith: { $arrayToObject: $items2 } }
] )阶段1
$addFields阶段添加了一个新的obj文档字段该字段将关键字k定义为区域值将值v定义为该区域的数量。例如
{ _id : 1, quarter : 2019Q1, region : A, qty : 400, obj : { k : A, v : 400 } }阶段2
$group阶段按季度分组并使用$push将obj字段累加到一个新的items数组字段中。例如
{ _id : 2019Q1, items : [ { k : A, v : 400 }, { k : B, v : 550 }, { k : C, v : 1000 } ] }阶段3
$project阶段使用$concatArrays创建一个新数组items2其中包括_id信息和items数组中的元素
{ _id : 2019Q1, items2 : [ { k : _id, v : 2019Q1 }, { k : A, v : 400 }, { k : B, v : 550 }, { k : C, v : 1000 } ] }阶段4
$replaceWith使用$arrayToObject将items2转换成文档使用k、v键值对并将文档输出到下一阶段。例如
{ _id : 2019Q1, A : 400, B : 550, C : 1000 }最终的执行结果
{ _id : 2019Q1, A : 400, B : 550, C : 1000 }
{ _id : 2019Q2, A : 660, B : 500, C : 1200 }使用$$ROOT创建的新文档和默认文档替换
创建contacts集合
db.contacts.insertMany( [{ _id : 1, name: Fred, email: fredexample.net },{ _id : 2, name: Frank N. Stine, cell: 012-345-9999 },{ _id : 3, name: Gren Dell, cell: 987-654-3210, email: beoexample.net }
] )下面使用$replaceWith和$mergeObjects输出当前文档中缺失字段的默认值
db.contacts.aggregate( [{ $replaceWith:{ $mergeObjects:[{ _id: , name: , email: , cell: , home: },$$ROOT]}}
] )返回下面的文档
{_id: 1,name: Fred,email: fredexample.net,cell: ,home:
},
{_id: 2,name: Frank N. Stine,email: ,cell: 012-345-9999,home:
},
{_id: 3,name: Gren Dell,email: beoexample.net,cell: ,home: 987-654-3210
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/920915.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!