0

How to update Single object in array of objects by rules query is that object must by in document that has translation_key == as input that i read and object must have same language (id) if not i must create new object . ( mongodb, mongoose)

Language are required and uniueq in translations

{
    "_id" : ObjectId("5bfd5324725fb12bc4863cd8"),
    "deleted" : false,
    "deleted_at" : null,
    "noAuth" : false,
    "hidden" : false,
    "translation_key" : "Standard",
    "translation_type" : "text",
    "translation" : [ 
        {
            "system" : {
                "android" : false,
                "ios" : false,
                "web" : false,
                "api" : false
            },
            "language" : ObjectId("5bf52e06edb9902e2113d8b3"),
            "text":'',
            "active" : false,
            "prepared_string" : false,
            "_id" : ObjectId("5bfd910c2998d929644abd90"),
            "params" : []
        }, 
        {
            "system" : {
                "android" : false,
                "ios" : false,
                "web" : true,
                "api" : false
            },
            "active" : false,
            "prepared_string" : false,
            "_id" : ObjectId("5bfdb5d22998d929644ac2af"),
            "language" : ObjectId("5bf52e06edb9902e6471d8c1"),
            "text" : "Standard",
            "app_version" : "2.0",
            "params" : []
        }
    ],
    "create_date" : ISODate("2018-11-27T14:22:28.635Z"),
    "update_date" : ISODate("2018-11-27T14:22:28.635Z"),
    "__v" : 3
}

1 Answer 1

2
Model.findOneAndUpdate(
            {
              _id: "document_id",
              "translation.language": "languageId",
            },
            {
              $set: {
                "translation.$.text: "text",
                "translation.$.active": "status",
                "translation.$.system.android" : true
            },
            { new: true, upsert: true, }
          )
          .lean();
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.