How to transform MongoDB document to array -
my returned document looks this:
{ "_id": "57terjrtgutgsjj", "eventlist": [ { "eventid": "5346a816e4b0efc8bd9759b5", "riskscore": 0, "entity": { "entityid": "5346a816e4b0efc8bd9759b6", "name": "src ip", "value": "209.178.49.9", "type": "ip", "direction": "s", "iskey": "true" } } ] }
i want result in format:
{ "_id": "57terjrtgutgsjj", "eventlist": [ { "eventid": "5346a816e4b0efc8bd9759b5", "riskscore": 0, "entity": [ { "entityid": "5346a816e4b0efc8bd9759b6", "name": "src ip", "value": "209.178.49.9", "type": "ip", "direction": "s", "iskey": "true" } ] } ] }
the difference between first , second document "entity" field simple document in first case whereas array in second case. possible convert simple document 1 of elements inside array.obviously there 1 object inside array, doesn't matter requirements.so can please tell if possible.
thanks in advance.
i question whether want , take @ positional $
operator documentation reason why nested arrays not news if want update them.
but if decide want following form suit, noting of course need obtain document content first , otherwise use $unset
operator:
db.collection.find({ "_id": "57terjrtgutgsjj" }).foreach(function(doc) { db.collection.update( { "_id": doc._id }, { "$unset": { "entity": 1 } } ); db.collection.update( { "_id": doc._id }, { "$push": { "entity": doc.entity } } ); )
so 1 update, no. can "bulk updates" available 2.6 release of mongodb, benefit negated way need process in bulk. applied, should able see sort of coding pattern need used.
Comments
Post a Comment