pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<i id='9MllW'><tr id='9MllW'><dt id='9MllW'><q id='9MllW'><span id='9MllW'><b id='9MllW'><form id='9MllW'><ins id='9MllW'></ins><ul id='9MllW'></ul><sub id='9MllW'></sub></form><legend id='9MllW'></legend><bdo id='9MllW'><pre id='9MllW'><center id='9MllW'></center></pre></bdo></b><th id='9MllW'></th></span></q></dt></tr></i><div class="dn5ttzf" id='9MllW'><tfoot id='9MllW'></tfoot><dl id='9MllW'><fieldset id='9MllW'></fieldset></dl></div>
  • <small id='9MllW'></small><noframes id='9MllW'>

      <tfoot id='9MllW'></tfoot>
        <bdo id='9MllW'></bdo><ul id='9MllW'></ul>

    1. <legend id='9MllW'><style id='9MllW'><dir id='9MllW'><q id='9MllW'></q></dir></style></legend>
      1. 將數(shù)組中的字符串值連接到 MongoDB 的單個(gè)字段中

        Concatenate string values in array in a single field in MongoDB(將數(shù)組中的字符串值連接到 MongoDB 的單個(gè)字段中)
      2. <tfoot id='YAL2R'></tfoot>
        <i id='YAL2R'><tr id='YAL2R'><dt id='YAL2R'><q id='YAL2R'><span id='YAL2R'><b id='YAL2R'><form id='YAL2R'><ins id='YAL2R'></ins><ul id='YAL2R'></ul><sub id='YAL2R'></sub></form><legend id='YAL2R'></legend><bdo id='YAL2R'><pre id='YAL2R'><center id='YAL2R'></center></pre></bdo></b><th id='YAL2R'></th></span></q></dt></tr></i><div class="rfdvj55" id='YAL2R'><tfoot id='YAL2R'></tfoot><dl id='YAL2R'><fieldset id='YAL2R'></fieldset></dl></div>
          <tbody id='YAL2R'></tbody>

          <small id='YAL2R'></small><noframes id='YAL2R'>

            • <bdo id='YAL2R'></bdo><ul id='YAL2R'></ul>
              <legend id='YAL2R'><style id='YAL2R'><dir id='YAL2R'><q id='YAL2R'></q></dir></style></legend>

                  本文介紹了將數(shù)組中的字符串值連接到 MongoDB 的單個(gè)字段中的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  假設(shè)我有一系列格式如下的文檔:

                  Suppose that I have a series of documents with the following format:

                  {
                      "_id": "3_0",
                      "values": ["1", "2"]
                  }
                  

                  我想獲得在單個(gè)字段中連接的數(shù)組值的投影:

                  and I would like to obtain a projection of the array's values concatenated in a single field:

                  {
                      "_id": "3_0",
                      "values": "1_2"
                  }
                  

                  這可能嗎?我試過 $concat 但我想我不能使用 $values 作為 $concat 的數(shù)組.

                  Is this possible? I have tried $concat but I guess I can't use $values as the array for $concat.

                  推薦答案

                  在現(xiàn)代 MongoDB 版本中,您可以.您仍然不能直接"將數(shù)組應(yīng)用到 $concat,但是您可以使用 $reduce 處理數(shù)組元素并生成:

                  In Modern MongoDB releases you can. You still cannot "directly" apply an array to $concat, however you can use $reduce to work with the array elements and produce this:

                  db.collection.aggregate([
                    { "$addFields": {
                      "values": { 
                        "$reduce": {
                          "input": "$values",
                          "initialValue": "",
                          "in": {
                            "$cond": {
                              "if": { "$eq": [ { "$indexOfArray": [ "$values", "$$this" ] }, 0 ] },
                              "then": { "$concat": [ "$$value", "$$this" ] },
                              "else": { "$concat": [ "$$value", "_", "$$this" ] }
                            }    
                          }
                        }        
                      }
                    }}
                  ])
                  

                  當(dāng)然結(jié)合 $indexOfArray 當(dāng)它是數(shù)組的第一個(gè)"索引時(shí),"_" 下劃線連接".

                  Combining of course with $indexOfArray in order to not "concatenate" with the "_" underscore when it is the "first" index of the array.

                  我的額外愿望"也得到了$sum的回答:

                  Also my additional "wish" has been answered with $sum:

                  db.collection.aggregate([
                    { "$addFields": {
                      "total": { "$sum": "$items.value" }
                    }}
                  ])
                  

                  <小時(shí)>

                  一般來說,這種類型的聚合運(yùn)算符會(huì)使用一組項(xiàng)目.這里的區(qū)別在于它表示編碼表示中提供的aguments"數(shù)組",而不是當(dāng)前文檔中存在的數(shù)組元素".


                  This kind of gets raised a bit in general with aggregation operators that take an array of items. The distinction here is that it means an "array" of "aguments" provided in the coded representation a opposed to an "array element" present in the current document.

                  真正實(shí)現(xiàn)文檔中存在的數(shù)組中的項(xiàng)目連接的唯一方法是執(zhí)行某種 JavaScript 選項(xiàng),例如 mapReduce:

                  The only way you can really do the kind of concatenation of items within an array present in the document is to do some kind of JavaScript option, as with this example in mapReduce:

                  db.collection.mapReduce(
                      function() {
                          emit( this._id, { "values": this.values.join("_") } );
                      },
                      function() {},
                      { "out": { "inline": 1 } }
                  )
                  

                  當(dāng)然,如果您實(shí)際上并未聚合任何內(nèi)容,那么最好的方法可能是在您的客戶端代碼中簡單地執(zhí)行加入"操作,以便對查詢結(jié)果進(jìn)行后處理.但是,如果它需要跨文檔用于某種目的,那么 mapReduce 將是您唯一可以使用它的地方.

                  Of course if you are not actually aggregating anything, then possibly the best approach is to simply do that "join" operation within your client code in post processing your query results. But if it needs to be used in some purpose across documents then mapReduce is going to be the only place you can use it.

                  我可以添加例如"我希望這樣的東西可以工作:

                  I could add that "for example" I would love for something like this to work:

                  {
                      "items": [
                          { "product": "A", "value": 1 },
                          { "product": "B", "value": 2 },
                          { "product": "C", "value": 3 }
                      ]
                  }
                  

                  總的來說:

                  db.collection.aggregate([
                      { "$project": {
                          "total": { "$add": [
                              { "$map": {
                                  "input": "$items",
                                  "as": "i",
                                  "in": "$$i.value"
                              }}
                          ]}
                      }}
                  ])
                  

                  但它不起作用,因?yàn)?$add 需要參數(shù)而不是文檔中的數(shù)組.嘆!:(.對此的設(shè)計(jì)"推理的一部分可以這樣說,僅僅因?yàn)?它是一個(gè)數(shù)組或列表"的奇異值從轉(zhuǎn)換的結(jié)果中傳入,它不是保證"那些是實(shí)際上是運(yùn)算符期望的有效"奇異數(shù)值類型值.至少在當(dāng)前實(shí)現(xiàn)的類型檢查"方法中不是.

                  But it does not work that way because $add expects arguments as opposed to an array from the document. Sigh! :(. Part of the "by design" reasoning for this could be argued that "just because" it is an array or "list" of singular values being passed in from the result of the transformation it is not "guaranteed" that those are actually "valid" singular numeric type values that the operator expects. At least not at the current implemented methods of "type checking".

                  這意味著現(xiàn)在我們?nèi)匀槐仨氝@樣做:

                  That means for now we still have to do this:

                  db.collection.aggregate([
                     { "$unwind": "$items" },
                     { "$group": {
                         "_id": "$_id",
                          "total": { "$sum": "$items.value" }
                     }}
                  ])
                  

                  遺憾的是,也沒有辦法應(yīng)用這樣的分組運(yùn)算符來連接字符串.

                  And also sadly there would be no way to apply such a grouping operator to concatenate strings either.

                  因此,您可以希望對此進(jìn)行某種更改,或者希望進(jìn)行一些更改,以允許在 $map 以某種方式操作.更好的是,新的 $join 操作也會(huì)受到歡迎.但這些在撰寫本文時(shí)不存在,并且可能在未來一段時(shí)間內(nèi)不會(huì)存在.

                  So you can hope for some sort of change on this, or hope for some change that allows an externally scoped variable to be altered within the scope of a $map operation in some way. Better yet a new $join operation would be welcome as well. But these do not exist as of writing, and probably will not for some time to come.

                  這篇關(guān)于將數(shù)組中的字符串值連接到 MongoDB 的單個(gè)字段中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Use IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
                  anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 項(xiàng)目中不起作用)
                  Ionic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用異步數(shù)據(jù)更新 Observable)
                  Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
                  In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何創(chuàng)建使用 Ionic 組件的自定義指令?)
                  Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(將 ViewChild 用于動(dòng)態(tài)元素 - Angular 2 amp;離子2)

                    <small id='JVuHy'></small><noframes id='JVuHy'>

                    <tfoot id='JVuHy'></tfoot>
                      <bdo id='JVuHy'></bdo><ul id='JVuHy'></ul>

                      • <i id='JVuHy'><tr id='JVuHy'><dt id='JVuHy'><q id='JVuHy'><span id='JVuHy'><b id='JVuHy'><form id='JVuHy'><ins id='JVuHy'></ins><ul id='JVuHy'></ul><sub id='JVuHy'></sub></form><legend id='JVuHy'></legend><bdo id='JVuHy'><pre id='JVuHy'><center id='JVuHy'></center></pre></bdo></b><th id='JVuHy'></th></span></q></dt></tr></i><div class="jpv5lxt" id='JVuHy'><tfoot id='JVuHy'></tfoot><dl id='JVuHy'><fieldset id='JVuHy'></fieldset></dl></div>
                            <tbody id='JVuHy'></tbody>
                          <legend id='JVuHy'><style id='JVuHy'><dir id='JVuHy'><q id='JVuHy'></q></dir></style></legend>

                            主站蜘蛛池模板: 广州网站建设_小程序开发_番禺网站建设_佛山网站建设_粤联网络 | 上海软件开发-上海软件公司-软件外包-企业软件定制开发公司-咏熠科技 | 磁力抛光机_磁力研磨机_磁力去毛刺机-冠古设备厂家|维修|租赁【官网】 | 热熔胶网膜|pes热熔网膜价格|eva热熔胶膜|热熔胶膜|tpu热熔胶膜厂家-苏州惠洋胶粘制品有限公司 | 高压分散机(高压细胞破碎仪)百科-北京天恩瀚拓 | 基业箱_环网柜_配电柜厂家_开关柜厂家_开关断路器-东莞基业电气设备有限公司 | 板式换网器_柱式换网器_自动换网器-郑州海科熔体泵有限公司 | 真空泵维修保养,普发,阿尔卡特,荏原,卡西亚玛,莱宝,爱德华干式螺杆真空泵维修-东莞比其尔真空机电设备有限公司 | 宽带办理,电信宽带,移动宽带,联通宽带,电信宽带办理,移动宽带办理,联通宽带办理 | 烟台金蝶财务软件,烟台网站建设,烟台网络推广 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 合肥通道闸-安徽车牌识别-人脸识别系统厂家-安徽熵控智能技术有限公司 | 2025黄道吉日查询、吉时查询、老黄历查询平台- 黄道吉日查询网 | 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 煤粉取样器-射油器-便携式等速飞灰取样器-连灵动 | 济南保安公司加盟挂靠-亮剑国际安保服务集团总部-山东保安公司|济南保安培训学校 | 超声波乳化机-超声波分散机|仪-超声波萃取仪-超声波均质机-精浩机械|首页 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 湖南教师资格网-湖南教师资格证考试网 | 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 深圳市八百通智能技术有限公司官方网站 | 山东锐智科电检测仪器有限公司_超声波测厚仪,涂层测厚仪,里氏硬度计,电火花检漏仪,地下管线探测仪 | 货车视频监控,油管家,货车油管家-淄博世纪锐行电子科技 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 | 盛源真空泵|空压机-浙江盛源空压机制造有限公司-【盛源官网】 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 头条搜索极速版下载安装免费新版,头条搜索极速版邀请码怎么填写? - 欧远全 | 空气净化器租赁,空气净化器出租,全国直租_奥司汀净化器租赁 | 机房监控|动环监控|动力环境监控系统方案产品定制厂家 - 迈世OMARA | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 硅胶布|电磁炉垫片|特氟龙胶带-江苏浩天复合材料有限公司 | 清洁设备_洗地机/扫地机厂家_全自动洗地机_橙犀清洁设备官网 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 防腐储罐_塑料储罐_PE储罐厂家_淄博富邦滚塑防腐设备科技有限公司 | 服务器之家 - 专注于服务器技术及软件下载分享 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 红外光谱仪维修_二手红外光谱仪_红外压片机_红外附件-天津博精仪器 |