0

{
roomId: "id",
questions:{
     q1:{
        user1:"user1's work"
        }
          }
}

I'm trying to query mongodb with multiple conditions, that roomId has to match, and questions must be q1, and in q1 there must be a user1.

Here's what I've tried so far. Using and operator, but doesn't seems to work.For now I'm using find, as I read in the docs that updateMany has the same query selector as find.

const result = await collection.find({
      $and: [
        {
          roomId: roomId,
        },
        {
          questions: {
            currentQuestion: {
              userName,
            },
          },
        },
      ],
    });

My schema:


{
roomId: "id",
roomName:"roomName",
questions:{
     question1:{
        user1:"user1's work",

        userN: "userN's work"
        },

      questionN:{
        user1:"",
        userN:""
         }

          }
}

My expected input , (roomId, currentQuestion, userName) for query conditions,"userWork" to be inserted to what's under userName (user1 - userN). Expected output, that the user's work gets updated with "userWork", under the right room, right currentQuestion and the right user.

1 Answer 1

1

You need this query I think:

db.collection.find({
  "roomId": "id",
  "questions.q1.user1": {
    $exists: true
  }
})

This query find a roomId which match your 'id' and then, check if exists the element questions.q1.user1.

Mongo playground example here

PS: You said update but... what do you want to update?

Assuming your schema is like

{
  roomId: "id",
  questions: {
    q1: {
      user1: "user1's work",
      currentQuestion: "currentQuestion1"
    }
  }
}

Then, the query to update the currentQuestion field whose match the id and has existing questions.q1.user1 is this:

db.collection.update({
  "roomId": "id",
  "questions.q1.user1": {
    $exists: true
  }
},
{
  "$set": {
    "questions.q1.currentQuestion": "new question"
  }
})

Example here

Note that if currentQuestion is one level up, you only have to modify $set object.

If you are not asking for this, please provide a complete schema, example input and expected output.

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

3 Comments

Thank you! I've edited my question. No, the current question(number) is actually determined on the client side. Current question(number) is used to search for where to find user under question number. Is there a way I can replace with variables for "questions.q1.user", or do I need to create a custom string function?
Is possible querying using variables as fields. Check this question.
I tried to assign "questions.q1.user1" = queryString, but when I use variable queryString it doesn't seem work.

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.