0

regards, suppose we have the following documents in my collection called inventory and I'm trying to show only the incoming movements inventory:

[
    {
      "_id": "638150ee268d890e9983881f",
      "movements": {
        "entries": [
          {
            "code": 4,
            "created_at": "2022-11-25T23:31:38.523Z",
            "created_by": "userId",
            "type": "Ingreso",
            "observation": "It is a long established fact that a reader will be distracted",
            "old_quantity": 0,
            "new_quantity": 10,
            "total_quantity": 10
          },...
        ]
      }
    },
    {
      "_id": "63810f179db70fea1263dea5",
      "movements": {
        "entries": [
          {
            "code": 1,
            "created_at": "2022-11-25T18:52:54.528Z",
            "created_by": "userId",
            "type": "Ingreso",
            "observation": "It is a long established fact that a reader will be distracted",
            "old_quantity": 0,
            "new_quantity": 100,
            "total_quantity": 100
          },...
        ]
      }
    }
  ]

This is the response request from my api, until here its ok, but I would like to show something like this:

[
    {
      "code": 4,
      "created_at": "2022-11-25T23:31:38.523Z",
      "created_by": "userId",
      "type": "Ingreso",
      "observation": "It is a long established fact that a reader will be distracted",
      "old_quantity": 0,
      "new_quantity": 10,
      "total_quantity": 10
    },
    {
      "code": 5,
      "created_at": "2022-11-25T23:31:38.523Z",
      "created_by": "userId",
      "type": "Ingreso",
      "observation": "It is a long established fact that a reader will be distracted",
      "old_quantity": 10,
      "new_quantity": 20,
      "total_quantity": 30
    }, 
    {
      "code": 1,
      "created_at": "2022-11-25T18:52:54.528Z",
      "created_by": "userId",
      "type": "Ingreso",
      "observation": "It is a long established fact that a reader will be distracted",
      "old_quantity": 0,
      "new_quantity": 100,
      "total_quantity": 100
    },
    {
      "code": 2,
      "created_at": "2022-11-25T18:52:54.528Z",
      "created_by": "userId",
      "type": "Ingreso",
      "observation": "It is a long established fact that a reader will be distracted",
      "old_quantity": 100,
      "new_quantity": 100,
      "total_quantity": 200
    },...
  ]

I don't know if it's possible to do it this way. If it is not possible at least I would expect to suppress the embedded elements of the document; leaving in a first level entries array. Something like this:

[
    {
      "_id": "638150ee268d890e9983881f",
      "entries": [
        {
          "code": 4,
          "created_at": "2022-11-25T23:31:38.523Z",
          "created_by": "userId",
          "type": "Ingreso",
          "observation": "It is a long established fact that a reader will be distracted",
          "old_quantity": 0,
          "new_quantity": 10,
          "total_quantity": 10
        }...
      ]
    },
    {
      "_id": "63810f179db70fea1263dea5",
      "entries": [
        {
          "code": 1,
          "created_at": "2022-11-25T18:52:54.528Z",
          "created_by": "userId",
          "type": "Ingreso",
          "observation": "It is a long established fact that a reader will be distracted",
          "old_quantity": 0,
          "new_quantity": 100,
          "total_quantity": 100
        }...
      ]
    }
  ];

As each document contains other information, for the purposes of this example I am only focusing on displaying the incoming inventory movement data so I have implemented the following and it works, but not as I would expect (mentioned above):

return this.connect().then((db) => {
      return db
        .collection(collection)
        .find({ is_deleted: false })
        .project({ movements: { entries: 1 } })
        .sort({ _id: -1 })
        .toArray();
    });

1 Answer 1

1

In the projection, set entries field with the value of movements.entries.

return this.connect().then((db) => {
      return db
        .collection(collection)
        .find({ is_deleted: false })
        .project({
          entries: "$movements.entries"
        })
        .sort({ _id: -1 })
        .toArray();
    });

Demo @ Mongo Playground

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, the property "data" is just the response from API, I've changed the result from MongoDB
this returns only the id's of every array [ { "_id": "63810f179db70fea1263dea5" }, { "_id": "638150ee268d890e9983881f" } ]
Sorry typo error, should be movements.entries.
yesssssssssssssssss that was correct! I was reading and found the 's' was missing. Thanks!

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.