Reshape all the documents in the collection

前端 未结 1 1365
心在旅途
心在旅途 2021-01-14 06:50

I have the following structure in my documents:

{
    \"_id\" : 1,
    \"item\" : {
        \"name\" : \"abc\",
        \"price\" : 10,
        \"quantity\"          


        
相关标签:
1条回答
  • 2021-01-14 07:30

    You can use the aggregation especially the $projectoperator for that. The $out operator let you write the result in another collection.

    db.collection.aggregate([
        { "$project": {
            "_id": "$_id", 
            "name": "$item.name",
            "price": "$item.price", 
            "quantity": "$item.quantity", 
            "date": "$item.date"}
        }, 
        { "$out": "collection"}
    ])
    

    You documents now look like this:

    {
        "_id" : 1,
        "name" : "abc",
        "price" : 10,
        "quantity" : 2,
        "date" : ISODate("2014-03-01T08:00:00Z")
    }
    

    You can also overwrite the pre-existing collection by giving the new results collection the same name but this.

    0 讨论(0)
提交回复
热议问题