Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

php - mongodb get _id as string in find query

Here I have created a collection with a single document

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

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 and not _id
  2. ObjectId("562a6300bbc948a4315f3abc") vs "562a6300bbc948a4315f3abc"

    { "id" : "562a6300bbc948a4315f3abc" }

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" } } }
])

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"] }
              }
            }
          }
        }] 
      ]
    }
  }}
])

Which would return:

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

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

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.

The shell has a .map() method

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

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
})

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.


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 })
},{ })

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...