- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我必须过滤评论中仅包含状态 C 的对象(如果至少只有评论具有状态 C,则应单独打印该对象)我尝试使用数组过滤器,但我没有得到准确的结果
{
"_id" : ObjectId("5b8f84379f432a42383a85bb"),
"projectID" : ObjectId("00000000e614c33390237ce3"),
"inspection_data" : [
{
"locationAspects" : [
{
"aspectname" : "Ground floor",
"comments" : [
{
"status" :"C",
"comment" : [
"good"
],
"_id" : ObjectId("5b8f84379f16400f884d9974")
}
],
"_id" : ObjectId("5b8f84379f16400f884d9975")
},
{
"aspectname" : "Second floor",
"comments" : [
{
"status" :"P",
"comment" : [
"nothing"
],
"_id" : ObjectId("5b8f84379f16400f884d9971")
}
],
"_id" : ObjectId("5b8f84379f16400f884d9972")
},
],
"published_date" : ISODate("2018-09-05T07:22:31.017Z"),
"_id" : ObjectId("5b8f84379f16400f884d9976")
},
{
"locationAspects" : [
{
"aspectname" : "Ground floor",
"comments" : [
{
"status" :"P",
"comment" : [
"good"
],
"_id" : ObjectId("5b8f84379f16400f884d9974")
}
],
"_id" : ObjectId("5b8f84379f16400f884d9975")
}
],
"published_date" : ISODate("2018-09-05T07:22:31.017Z"),
"_id" : ObjectId("5b8f84379f16400f884d9976")
}
]
现在检查数据有两个对象,但一个对象只包含评论状态c,所以应该打印
预期结果
[ {
"locationAspects" : [
{
"aspectname" : "Ground floor",
"comments" : [
{
"status" :"C",
"comment" : [
"good"
],
"_id" : ObjectId("5b8f84379f16400f884d9974")
}
],
"_id" : ObjectId("5b8f84379f16400f884d9975")
},
{
"aspectname" : "Second floor",
"comments" : [
{
"status" :"P",
"comment" : [
"nothing"
],
"_id" : ObjectId("5b8f84379f16400f884d9971")
}
],
"_id" : ObjectId("5b8f84379f16400f884d9972")
},
],
"published_date" : ISODate("2018-09-05T07:22:31.017Z"),
"_id" : ObjectId("5b8f84379f16400f884d9976")
}]
如果至少有一个评论状态为 C,则以上对象只有状态 C,该对象必须单独显示
最佳答案
你需要$filter处理 inspection_data
。事情变得复杂,因为你有多层嵌套,所以在你申请之前 $in您需要获取单个 inspection_data
的所有状态的数组。要实现这一点,您可以使用像 "$$data.locationAspects.comments.status"
这样的直接路径,但它会返回一个数组,如下所示:
[ [ "C" ], [ "P" ] ], [ [ "P" ] ] ]
因此您必须展平该数组,这可以使用 $reduce 来实现和 $concatArrays .尝试:
db.col.aggregate([
{ $match: { projectID: ObjectId("00000000e614c33390237ce3") } },
{
$project: {
filtered_inspection_data: {
$filter: {
input: "$inspection_data",
as: "data",
cond: {
$let: {
vars: {
statuses: {
$reduce: {
input: "$$data.locationAspects.comments.status",
initialValue: [],
in: { $concatArrays: [ "$$this", "$$value" ] }
}
}
},
in: { $in: [ "C", "$$statuses" ] }
}
}
}
}
}
}
])
编辑:要匹配 "C"
或 "P"
,您可以使用替换 { $in: [ "C", "$$statuses"] }
包含以下行
{ $or: [ { $in: [ "C", "$$statuses" ] }, { $in: [ "P", "$$statuses" ] } ] }
关于node.js - 使用 Mongoose 使用 arrayFilters 查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52516252/
我试图做一个匹配一些嵌套数组条件的 mongodb 查询,但没有成功...... 文件: [ { "_id":1, "name":"foo", "games"
我的查询是关于更新以数组形式存储的多个子文档。 环境:我的电脑上安装了MongoDB shell版本v3.6.3、python 3.6.9和pymongo 3.6.1。 架构示例: dept : {
我在 Mongodb 3.6 上运行,带有 mongo 驱动程序 3.4.3 和 spring data mongo 1.5.10。下面是我的文档结构 { "_id": 12345, "_cl
这里是 knockout 新手。我尝试使用 ko.utils.arrayFilter 但当我使用它时它似乎没有更新。我使用与 arrayForEach 相同的方法,所以我不确定这里出了什么问题。使用
我在 mongo 中存储了以下文件: { "_id" : ObjectId("5d1a08d2329a3c1374f176df"), "associateID" : "1234567", "assoc
我试图通过忽略已经在先前列表中的用户来过滤数组: FullList: Tom, Tim, Jim, Jill UsersList: Tom, Jill 在另一位成员的帮助下,我能够使用这个数组过滤器,
假设文档如下: { "group_id": 1, "group_token": "3bu48xp9ok5", "group_passward": "0000", "admin": "F
我见过像 this 这样的例子或this SO question我们可以轻松地使用位置 $ 运算符来更新数组中的对象。 我正在尝试做同样的事情,但我必须使用对象子属性。 示例: // Data mod
将查询转换为 Node 。 arrayfilter 在哪个版本的 mongoose 中工作或者如何在 Node 应用程序中运行这些 db.getCollection('student').update
我正在尝试使用 arrayFilters 更新嵌套数组,根据文档,它似乎应该可以工作,但我遇到了错误: Error parsing array filter: Expected a single to
大家好,我正在尝试向一个项目添加一个过滤系统,该系统允许用户根据他们在输入字段中键入的内容来过滤一组位置,所发生的情况是它会自动填充 5 组对象,而且用户输入的内容并没有真正执行任何操作,并且我从网站
我使用 $[] 的更新失败。架构: tables: { type: [ { tableID: String, numberOfC
在批量操作中使用 arrayFilters 执行 NodeJS/MongoDB updateOne 的正确语法是什么? MongoDB 版本:4.2.5 NodeJS 版本:12.x npm mong
所以我最近使用 knockout.js 将我想要的过滤系统插入到我的项目中。我将以下代码插入到我的项目中。 self.pointsFilter = ko.computed(function(){
我必须过滤评论中仅包含状态 C 的对象(如果至少只有评论具有状态 C,则应单独打印该对象)我尝试使用数组过滤器,但我没有得到准确的结果 { "_id" : ObjectId("5b8
我有一个看起来像这样的 observableArray: this.menuItems = ko.observableArray([ { name: "level1", subItems: [
就目前而言,我能够使用 Mongo shell 实现此功能,但在使用 Mongoose 的 Node.js 内部嵌套数组时遇到问题。 成功的Mongo shell命令: db.test.update(
这个问题在这里已经有了答案: How to Update Multiple Array Elements in mongodb (15 个回答) 3年前关闭。 我问过 question几天前关于更新嵌
大家好,我正在尝试创建一个应用程序,在进行 knockout 搜索时将适当的标记设置为可见。 应用程序基本上是。当有人搜索它下面的列表时,过滤列表并仅使与过滤列表相关联的标记在 map 上可见。我已经
我正在使用 Spring boot 2.2.1.RELEASE。如何使用 Spring MongoOperation 编写以下 MongoDB 更新查询 db.cities.updateOne(
我是一名优秀的程序员,十分优秀!