Does mango provide functionality for deconstructing document arrays for large datasets?

Similar to display / decrease, but vice versa. Mongo has a way to reformat the data. I have a collection in the following format.

{ 
  {"token-id" : "LKJ8_lkjsd"
    "data": [
               {"views":100, "Date": "2015-01-01"},
               {"views":200, "Date": "2015-01-02"},
               {"views":300, "Date": "2015-01-03"},
               {"views":300, "Date": "2015-01-03"}
            ]
  }
}

I would like to process the entire collection in a new format. where each data point in the time series is a document mapped to an identifier, hopefully using some of the built-in mongo features like map reduction. If not; I would appreciate a strategy in which we can do this.

{
  { "token-id" : "LKJ8_lkjsd", "views": 100, "Date" : "2015-01-01"},
  { "token-id" : "LKJ8_lkjsd", "views": 200, "Date" : "2015-01-01"},
  { "token-id" : "LKJ8_lkjsd", "views": 300, "Date" : "2015-01-01"}
}
+4
source share
3 answers

aggregate , . db.collection.aggregate() .

 var result = db.test.aggregate( [ { $unwind : "$data" }, {$project: {_id:0, "token-id":1, "data":1}}])

    for(result.hasNext()){
     db.collection.insert(result.next());
    }
+3

$unwind , mongodb

db.yourcollection.aggregate( [ { $unwind : "$data" } ] )

unwind

> db.test.aggregate( [ { $unwind : "$data" }, {$project: {_id:0, "token-id":1, "data":1}}, {$out: "another"} ] )
> db.another.find()

_id, $unwind 4 _id (, , ) _id

,

{ "_id" : ObjectId("560599b1699289a5b754fab9"), "token-id" : "LKJ8_lkjsd", "data" : { "views" : 100, "Date" : "2015-01-01" } }
{ "_id" : ObjectId("560599b1699289a5b754faba"), "token-id" : "LKJ8_lkjsd", "data" : { "views" : 200, "Date" : "2015-01-02" } }
{ "_id" : ObjectId("560599b1699289a5b754fabb"), "token-id" : "LKJ8_lkjsd", "data" : { "views" : 300, "Date" : "2015-01-03" } }
{ "_id" : ObjectId("560599b1699289a5b754fabc"), "token-id" : "LKJ8_lkjsd", "data" : { "views" : 300, "Date" : "2015-01-03" } }
+3

According to your question with a large data set, then it $unwindcreates a slow query performance for this case, you should use $ map in aggregation to process the array , as shown below: data

db.collection.aggregate({
"$project": {
    "result": {
        "$map": {
            "input": "$data",
            "as": "el",
            "in": {
                "token-id": "$token-id",
                "views": "$$el.views",
                "Date": "$$el.Date"
            }
        }
    }
 }
}).pretty()
+3
source

Source: https://habr.com/ru/post/1609028/


All Articles