php - MongoDB Query to find out all the array elements of a collection -


I have a very large MongoDB document that keeps all kinds of data, I need to identify the fields which There are type arrays in the collection, so I can extract them from the fields displayed in the grid which I will populate.

My method now involves retrieving all field names

taken from the posted response

 < {Emit (key, null);}}, "less": function (key, content) {code} mr = db.runCommand {{"mapreduce": "product", "map": function (var key) { Return null;}, "out": "things" + "  

and such a query runs for each field

  Db.Product .find ({$ where: "Array.isArray (this.Orders)"}). Count ()   

If any area has been retrieved, The array is considered.

I do not think I need to run n + 2 questions (my collection has a number of different areas) and I would not like to hardcode the fields in the model. It's MongoDB's

Is there a better way to do this?

I made some minor amendments to the code given above:

  mr = db.runcommomm ({"mapreduce": "product", "map R ": function {} ({var (key, 1);} else {emit (key, 0);}}}, {if (Array.isArray (this [key]) "Reduce": function key, content {back come sum (content);}, "out": "product" + "_keys"})   

Now, mapper is a 1 key Will use Array, and 0 which does not have any. The reducer will add these so that when you check your end result:

  db [mr.result] .find ()   

You will see your The number of documents with which the number of documents with the number of array values ​​(and which are never for any arrays)

In this way you should tell which field has array type, just map-less job

-

To see it with just a few data:

  db.Product.insert ({"a": [1,2] , 3], "c": [1,2]}) db.Product.insert ({"a": 1, "b": 2}) db.Product.insert ({"a": 1, "c ": [2,3]})   

(run" mr = "code above now)

  db [mr.result] .find ({ {"_id": "_id", "value": 0} {"_id": "a", "value": 1} {"_id": "b", "value": 0} {"_id": " C "," value ": 2}    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -