3
    {Crs:{[
       {Cr: {
            "_id" : ObjectId("5a75baada0f20bd4e612d480"),
            "Number" : 400,
            "Page" : 24,
            "DC" : "NE",
        }},
       {Cr: {
            "_id" : ObjectId("5a75baada0f20bd4e612d489"),
            "Number" : 300,
            "Page" : 14,
            "DC" : "100",
        }},
 ]}}

I have this data that i model using aggregate and I would like to know how can i remove "DC" field from all elements of the array using aggregate.

  >db.crs.aggregate(
    [
        {$group : {_id : null, crs : {$push : {cr : "$$ROOT"}}}},
        {$project : {_id : 0}}
    ]
)

This is what did to model that data. I think its something related with $project.

4
  • Is that even valid JSON? Commented Feb 5, 2018 at 13:38
  • @chridam I'm really new in this so i didnt use any validate method. I just modeled the data that I imported from a csv file using aggregate. Commented Feb 5, 2018 at 13:46
  • Can you edit your question to show the actual JSON output that you get when you run the query db.crs.find({}).limit(2).pretty()? Commented Feb 5, 2018 at 14:47
  • @chridam Its kinda hard to do that because is this not my actual work, its just an example to understand better how this works. Commented Feb 5, 2018 at 14:56

1 Answer 1

2

First of all, your document is not a valid JSON schema. You might reconsider the following schema for that particular document.

{
    Crs:[
        {
            "_id" : ObjectId("5a75baada0f20bd4e612d480"),
            "Number" : 400,
            "Page" : 24,
            "DC" : "NE"
        },
        {
            "_id" : ObjectId("5a75baada0f20bd4e612d489"),
            "Number" : 300,
            "Page" : 14,
            "DC" : "100"
        }
    ]
}

Having above schema, you can easily remove DC field using $unset aggregate operator.

db.crs.aggregate([{$unset: 'Crs.DC'}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.