- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下结构:
{
"_id":"some_id",
"tweets":[
{
"id":"1077633161922785281",
"created_at":"Tue Dec 25 18:32:21 +0000 2018",
"favorite_count":1905,
"retweet_count":27,
"media":[
],
"text":"Chillin",
"avatar":"https://pbs.twimg.com/profile_images/1062579676508381184/gDukIs20_normal.jpg",
"name":"Chance Morris",
"display_name":null,
"user_id":"1087348778",
"likedBy":[
"some-random-dude"
],
"likes":1
}
]
}
如果likedBy
数组包含user_id
和$project
,我想$addFields
is_liked
隐藏数组
db().collection('gamers').aggregate([
{
$project: {
'tweets.likedBy': 0
}
},
{
$addFields: {
'tweets.is_liked': {
$map: {
input: "$tweets",
as: "tweet",
in: {
id: "$$tweet.id",
text: "$$tweet.text",
is_liked: { $in: [ "some-random-dude", { $ifNull: [ "$$tweet.likedBy", [] ] } ] }
}
}
}
}
} ])
问题是 $map
数组在每个项目中重复:截图:https://prnt.sc/lzrwln
基本上,最终结果是将 likedBy
数组替换为 is_liked: true/false
,因此我不必在 FE 中携带该数组来检查用户是否喜欢
最佳答案
您可以使用$mergeObjects将现有推文与 is_liked
字段合并,然后使用 $project要从最终结果中排除 likedBy
数组,请尝试:
db.gamers.aggregate([
{
$project: {
tweets: {
$map: {
input: "$tweets",
as: "tweet",
in: {
$mergeObjects: [
"$$tweet",
{ is_liked: { $in: [ "some-random-dude", { $ifNull: [ "$$tweet.likedBy", [] ] } ] } }
]
}
}
}
}
},
{
$project: {
"tweets.likedBy": 0
}
}
])
关于node.js - MongoDB $addFields 和 $in 总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53934300/
我想在查找结果中添加额外的字段,但我需要一个条件。每个用户都有一个关系状态(本地字段),应该将其添加到每个用户的查找结果中,但它会将每个“状态”添加到每个用户。 db.getCollection('r
我试图仅在满足特定条件时才添加字段,例如,如果数组大小 > 0 则使用 $addFields 添加字段“date”,否则不添加 当使用 $cond 时,它是一个 bool 值,所以我必须添加一个带有日
我目前正在尝试制作一个会倒计时到特定日期的 discord 机器人。现在我将参数传递到 .json 文件的文件夹中,其中仅存储事件名称和发布日期/事件日期。 bot.on('ready', () =>
设置自定义表单。 Magento 不使用 addField 的“value”属性。建议? 代码: $form = new Varien_Data_Form(array( 'id' => 'ed
使用 migrations ,我需要向模型添加一个新字段(外键)。我知道可以通过以下方式完成: migrations.AddField( model_name='MyModel
我是新的 Mongodb 开发人员,我写了 mongodb 聚合。我的领域 lampStatus 之一:“OFF”是 30 条记录,我正在使用“{ $match: {lampStatus : “OFF
我正在努力解决一个恼人的问题。我在查询中使用聚合,我需要加入一些集合,并且输出必须是一个对象。这是我的代码... usersModel.aggregate([ { $match
我是新的 Mongodb 开发人员,我写了 mongodb 聚合。我的领域 lampStatus 之一:“OFF”是 30 条记录,我正在使用“{ $match: {lampStatus : “OFF
我正在努力解决一个恼人的问题。我在查询中使用聚合,我需要加入一些集合,并且输出必须是一个对象。这是我的代码... usersModel.aggregate([ { $match
我将云中提供的视频的视频 ID 存储在我的 mongodb 集合中。当我的前端对该视频进行 API 调用时,我想根据该视频 ID 返回格式化的 URL。我有一个函数可以执行此操作,但我无法让它与 $a
我知道可以 .spliceFields() ,只要我指明要删除的索引和字段数即可。但是由于我有很多嵌入并且我希望它们拼接一个主嵌入中的每个字段,我想创建一个变量,其值等于主嵌入中的字段数,因为我可能会
我想弄清楚如何使用 $addFields我用来更新文档的函数中的操作。生成这个字段的逻辑很简单,我只是拉出数组中的最后一个对象,如下所示: { $addFields: { activeWo
对于 Magento 后端,我创建了一个 Varien_Form 来显示。 class MyNamespace_MyModule_Block_Adminhtml_MyModule_Edit_Tabs_
这个问题在这里已经有了答案: Retrieving specific fields using the Elasticsearch Java API (1 个回答) 关闭 7 年前。 这是这个问题的
我有一个 solrobject,我想添加一个字段。 当我尝试添加字段时它抛出 PHP Fatal error: Call to undefined method SolrObject::addF
我有以下结构: { "_id":"some_id", "tweets":[ { "id":"1077633161922785281",
我有这个数据结构声明: [ProtoContract] public class NotACollectionHolder { public NotACollection some_objec
我有以下查询。 db.Collection.aggregate([ { "$match":{ "index": 1
在我的 mongodb 查询中,我使用 $addFields 添加 ID 字段,该字段连接了其他三个字段。我的问题是,如果我将新添加的字段与我要查询的值匹配,我将得不到任何结果。对于其他领域,它们工作
MongoDb 版本 3.4.4 如何为数组快照中的每个对象聚合一个新键“total”,其值是“course”和“quantity”的乘积。 示例文档: { cur: "EUR", s
我是一名优秀的程序员,十分优秀!