1. <tfoot id='Jv9LC'></tfoot>

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

          <bdo id='Jv9LC'></bdo><ul id='Jv9LC'></ul>
        <i id='Jv9LC'><tr id='Jv9LC'><dt id='Jv9LC'><q id='Jv9LC'><span id='Jv9LC'><b id='Jv9LC'><form id='Jv9LC'><ins id='Jv9LC'></ins><ul id='Jv9LC'></ul><sub id='Jv9LC'></sub></form><legend id='Jv9LC'></legend><bdo id='Jv9LC'><pre id='Jv9LC'><center id='Jv9LC'></center></pre></bdo></b><th id='Jv9LC'></th></span></q></dt></tr></i><div id='Jv9LC'><tfoot id='Jv9LC'></tfoot><dl id='Jv9LC'><fieldset id='Jv9LC'></fieldset></dl></div>
        <legend id='Jv9LC'><style id='Jv9LC'><dir id='Jv9LC'><q id='Jv9LC'></q></dir></style></legend>

      2. mongodb 在查找查询中获取 _id 作为字符串

        时间:2023-10-15

        • <bdo id='RKOpg'></bdo><ul id='RKOpg'></ul>

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

                  <tbody id='RKOpg'></tbody>
              • <tfoot id='RKOpg'></tfoot>

                  <i id='RKOpg'><tr id='RKOpg'><dt id='RKOpg'><q id='RKOpg'><span id='RKOpg'><b id='RKOpg'><form id='RKOpg'><ins id='RKOpg'></ins><ul id='RKOpg'></ul><sub id='RKOpg'></sub></form><legend id='RKOpg'></legend><bdo id='RKOpg'><pre id='RKOpg'><center id='RKOpg'></center></pre></bdo></b><th id='RKOpg'></th></span></q></dt></tr></i><div id='RKOpg'><tfoot id='RKOpg'></tfoot><dl id='RKOpg'><fieldset id='RKOpg'></fieldset></dl></div>
                  <legend id='RKOpg'><style id='RKOpg'><dir id='RKOpg'><q id='RKOpg'></q></dir></style></legend>
                1. 本文介绍了mongodb 在查找查询中获取 _id 作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这里我创建了一个包含单个文档的集合

                  Here I have created a collection with a single document

                  db.getCollection('example').insert({"example":1});
                  

                  我尝试使用 Projection,但我找回了 _id.

                  I have tried to use Projection, and I get back the _id.

                  db.getCollection('example').find({"example":1},{"_id":1});
                  
                  {
                      "_id" : ObjectId("562a6300bbc948a4315f3abc")
                  }
                  

                  但是,我需要如下输出.

                  However, I need the below output as shown below.

                  1. id 而不是 _id
                  2. ObjectId("562a6300bbc948a4315f3abc") vs "562a6300bbc948a4315f3abc"

                  1. id and not _id
                  2. ObjectId("562a6300bbc948a4315f3abc") vs "562a6300bbc948a4315f3abc"

                  <代码>{id":562a6300bbc948a4315f3abc"}

                  虽然我可以在我的应用服务器(基于 PHP)上处理 #1 和 #2 以获得所需的输出,但我正在寻找是否有办法在从 mongo 本身查询时获得预期结果

                  Although I can process #1 and #2 on my app server(PHP based) to get the desired ouput, I am looking if there is a way to get the expected result on querying from mongo itself

                  推荐答案

                  MongoDB 4.0 添加了 $convert 聚合运算符和 $toString 别名,它允许你做到这一点:

                  MongoDB 4.0 adds the $convert aggregation operator and the $toString alias which allows you to do exactly that:

                  db.getCollection('example').aggregate([
                    { "$match": { "example":1 } },
                    { "$project": { "_id": { "$toString": "$_id" } } }
                  ])
                  

                  一个主要的用法很可能是使用 _id 值作为文档中的键".

                  A main usage would most likely be though to use the _id value as a "key" in a document.

                  db.getCollection('example').insertOne({ "a": 1, "b": 2 })
                  
                  db.getCollection('example').aggregate([
                    { "$replaceRoot": {
                      "newRoot": {
                        "$arrayToObject": [
                          [{ 
                            "k": { "$toString": "$_id" },
                            "v": {
                              "$arrayToObject": {
                                "$filter": {
                                  "input": { "$objectToArray": "$$ROOT" },
                                  "cond": { "$ne": ["$$this.k", "_id"] }
                                }
                              }
                            }
                          }] 
                        ]
                      }
                    }}
                  ])
                  

                  哪个会返回:

                  { 
                    "5b06973e7f859c325db150fd" : { "a" : 1, "b" : 2 }
                  }
                  

                  它清楚地显示了字符串,另一个例子也是如此.

                  Which clearly shows the string, as does the other example.

                  尽管通常有一种方法可以在从服务器返回文档时对光标进行转换".这通常是一件好事,因为 ObjectId 是一个 12 字节的二进制表示,而不是需要更多空间的 24 个字符的十六进制字符串".

                  Generally though there is usually a way to do "transforms" on the cursor as documents are returned from the server. This is usually a good thing since an ObjectId is a 12-byte binary representation as opposed to a 24 character hex "string" which takes a lot more space.

                  shell 有一个 .map() 方法

                  The shell has a .map() method

                  db.getCollection('example').find().map(d => Object.assign(d, { _id: d._id.valueOf() }) )
                  

                  NodeJS 有一个 Cursor.map() 可以做很多相同的事情:

                  And NodeJS has a Cursor.map() which can do much the same thing:

                  let cursor = db.collection('example').find()
                      .map(( _id, ...d }) => ({ _id: _id.toString(), ...d }));
                  
                  while ( await cursor.hasNext() ) {
                    let doc = cursor.next();
                    // do something
                  })
                  

                  同样的方法也存在于其他驱动程序中(只是不是 PHP),或者您可以迭代光标并转换内容,这更有可能是最好的做法.

                  And the same method exists in other drivers as well ( just not PHP ), or you can just iterate the cursor and transform the content as is more likely the best thing to do.

                  实际上,在 shell 中工作时,只需将整个游标结果添加到任何游标返回语句中,就可以轻松地将其简化为单个对象

                  In fact, whole cursor results can be reduced into a single object with great ease by simply adding to any cursor returning statement, when working in the shell

                  .toArray().reduce((o,e) => { 
                    var _id = e._id;
                    delete e._id;
                    return Object.assign(o, { [_id]: e })
                  },{ })
                  

                  或者对于像 nodejs 这样的完整 ES6 JavaScript 支持环境:

                  Or for full ES6 JavaScript supporting environments like nodejs:

                  .toArray().reduce((o,({ _id, ...e })) =>  ({ ...o, [_id]: e }),{ })
                  

                  非常简单的东西,没有需要在聚合框架中处理的复杂性.并且很可能以几乎相同的方式使用任何语言.

                  Really simple stuff without the complexity of what needs to process in the aggregation framework. And very possible in any language by much the same means.

                  这篇关于mongodb 在查找查询中获取 _id 作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:访问嵌套属性 json 下一篇:mongodb 中 OR 条件匹配的百分比

                  相关文章

                    <tfoot id='AqYWB'></tfoot>

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

                  1. <i id='AqYWB'><tr id='AqYWB'><dt id='AqYWB'><q id='AqYWB'><span id='AqYWB'><b id='AqYWB'><form id='AqYWB'><ins id='AqYWB'></ins><ul id='AqYWB'></ul><sub id='AqYWB'></sub></form><legend id='AqYWB'></legend><bdo id='AqYWB'><pre id='AqYWB'><center id='AqYWB'></center></pre></bdo></b><th id='AqYWB'></th></span></q></dt></tr></i><div id='AqYWB'><tfoot id='AqYWB'></tfoot><dl id='AqYWB'><fieldset id='AqYWB'></fieldset></dl></div>
                    <legend id='AqYWB'><style id='AqYWB'><dir id='AqYWB'><q id='AqYWB'></q></dir></style></legend>
                      <bdo id='AqYWB'></bdo><ul id='AqYWB'></ul>