MongoDB $ addFields和$ in总计

时间:2018-12-26 16:02:07

标签: node.js mongodb mongoose

我具有以下结构:

{  
   "_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
      }
   ]
}

如果$addFields数组包含is_likedlikedByuser_id,我想$project likedBy: 0隐藏数组

 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中携带该数组

1 个答案:

答案 0 :(得分:1)

您可以使用$mergeObjects将现有的tweet与(defun c:curbyourenthusiasm ( / ang ep1 ep2 sp1 sp2 ) (if (and (setq sp1 (getpoint "\nSpecify start point: ")) (setq ep1 (getpoint "\nSpecify end point: " sp1)) ) (progn (setq ang (- (angle sp1 ep1) (/ pi 2)) sp2 (trans (polar sp1 ang 0.5) 1 0) ep2 (trans (polar ep1 ang 0.5) 1 0) sp1 (trans sp1 1 0) ep1 (trans ep1 1 0) ) (entmake (list '(0 . "LINE") '(8 . "concrete") (cons 10 sp1) (cons 11 ep1))) (entmake (list '(0 . "LINE") '(8 . "concrete") (cons 10 sp2) (cons 11 ep2))) ) ) (princ) ) 字段合并,然后使用$project从最终结果中排除val hideFlag: Int = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY activity?.window?.decorView?.systemUiVisibility = hideFlag 数组,请尝试:

is_liked