Working with embedded documents in Typescript

Hello,

If we consider the following document that contains 1 embedded document:

{
    "product": {
       "type": "Dairy"
    },
    "weight": 2,
    "quantity": 1
}

In order to update the product’s type one could do the following:

{"product.type" : "Vegetable"}

However, let’s suppose we wanted to update the document dynamically, as many both embedded and non-embedded values as we prefer.

Ergo, an idea would be to build a query object:

const updateQuery = {
    "product": {
       "type": "Vegetable" //modify this
    },
    "weight": 3 //modify this too
}

and pass it as a parameter to the updateProductById() function:

const updateProductById = async(productId: string, updateQuery) =>   {

   await ProductsModel.findOneAndUpdate(
            {_id: productId},
            {$set: updateQuery},
            {new: true}
        ).exec()

However, this will overwrite the original embedded document, resulting in a regenerated embedded document’s _id key.

Thus, we would have to do the following instead:

const updatedProduct = await ProductsModel.findOneAndUpdate(
  { _id: productId },
  { "product.type": "Vegetable" },
  { new: true }
).exec()

That’s good for that one value only, but

  1. how do you use {"product.type" : "Vegetable"} dynamically in Javascript passed along with other, non-embedded values?

  2. Is it possible to somehow express this key- value pair in a Javascript object?

  3. finally, could you tell me please, how to describe this in a Typescript interface?

Thank you!

Mongoose is doing that, not mongo.

When you update with

You basically say set the field product of the root document to the object {type:Vegetable}.

When you update with

You basically say set the field type of the sub-object product.

Both type of updates are needed for different use-case.

For

see Find - $text search with $or + other optional values - #2 by steevej

I find it really strange that Mongo is advocating for embedded documents, a feature that is integral to its high performance philosophy, yet them being so clunky and uncomfortable to work with. The only use of embedded documents, that with its straightforwardness could match the ease and bliss of working on the document scope itself, is saving the data and forgetting about it or printing it along with the rest of the data. The moment one needs to cherry pick embedded properties, modify them, verify their uniqueness against the whole collection, suddenly they become less attractive.

I am grateful for your link you have provided, but I could not find a desired solution whereby it would be possible to construct a single object that could be documented by a Typescript interface as a JS object, an object that would allow to dynamically update the whole or a part of the document or/ and its embedded properties without resetting the embedded _id keys.

Mongoose and Typescript are in your way rather than helping to access MongoDB easily and its full flexibility. They even slow down your development because you have to write these extra specifications and find way to bypass them for use-case they do not handle well.

Like I wrote:

and by that, I mean:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.