How to flatten an array in mongodb?
Name:[["abc","xvz"]]
into this way
Name:["abc","xyz"]
without using aggregate.
Aggregate will display the result but it shouldn't write the result back to the document.
Not sure why you don't want to use aggregation as this is a trivial step using [$unwind][2]
. Here is a simple example. If you want to overwrite you could just use update with the results or send the output of aggregate
to a new collection with [$out][2]
.
Below is an example using the MongoDB Shell.
>
> use test
switched to db test
> db.test.insertOne({name:[['xyz','abc']]})
{
"acknowledged" : true,
"insertedId" : ObjectId("5dbc28bbefffaafb7d97d7f1")
}
> db.test.aggregate([{"$unwind" : "$name"}])
{ "_id" : ObjectId("5dbc28bbefffaafb7d97d7f1"), "name" : [ "xyz", "abc" ] }
>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.