Sunday, 28 May 2017

How to use not In for the embeded document in the same collection [duplicate]

This question already has an answer here:

I am trying to create a quiz apis for my mobile app but I got stuck while using filters in mongodb.I have a collection named questions and each time when a user answers a question I store the user information in an embedded document named userAnsweredList. Now, I do not want to fetch the answered question by the user.

Get question Url: https://example.com/apis/questions/user_id

response:

   [{
    "_id": "1234f423babe1c64df5ada44",
    "timestamp": "2017-05-26T09:23:47.608Z",
    "text": "question text 2",
    "level": "Expert",
    "category": "NodeJs",
    "imageUrl": "_images/0oo38haha3156.png",
    "hintText": "hint is not availabe",
    "correctAnswere": "yes",
    "__v": 2,
    "userAnsweredList": [
      {
        "user_id": "100",
        "timestamp": "2017-05-26T09:41:55.822Z",
        "answer": "Correct",
        "_id": "5927f863babe1c64df5adace"
      },
      {
        "user_id": "101",
        "timestamp": "2017-01-26T09:41:55.822Z",
        "answer": "Incorrect",
        "_id": "5927f863babe1c64df5adace"
      }
    ]
  },
  {
    "_id": "1234f423babe1c64df5ada44",
    "timestamp": "2017-05-26T09:23:47.608Z",
    "text": "question text 2",
    "level": "Expert",
    "category": "NodeJs",
    "imageUrl": "_images/0oo38haha3156.png",
    "hintText": "hint is not availabe",
    "correctAnswere": "yes",
    "__v": 2,
    "userAnsweredList": [
      {
        "user_id": "101",
        "timestamp": "2017-01-26T09:41:55.822Z",
        "answer": "Incorrect",
        "_id": "5927f863babe1c64df5adace"
      }
    ]
}]

Now if I call https://example.com/apis/questions/100 i should not get the first question and response should be

[{
        "_id": "1234f423babe1c64df5ada44",
        "timestamp": "2017-05-26T09:23:47.608Z",
        "text": "question text 2",
        "level": "Expert",
        "category": "NodeJs",
        "imageUrl": "_images/0oo38haha3156.png",
        "hintText": "hint is not availabe",
        "correctAnswere": "yes",
        "__v": 2,
        "userAnsweredList": [
          {
            "user_id": "101",
            "timestamp": "2017-01-26T09:41:55.822Z",
            "answer": "Incorrect",
            "_id": "5927f863babe1c64df5adace"
          }
        ]
    }]

here is my module file

//Answered Questions
var userAnswered = new Schema ({
  uid:{type: String,required:true},
  answer:{type:String,required:true},// Correct or wrong 
  timestamp : {type: Date},
})
//Questions
var QuestionSchema = new Schema ({
  text : {type: String, required:true},
  timestamp : {type: Date},
  level:{type:String,required:true},
  category:{type:String,required:true},
  imageUrl:{type:String,required:true},
  hintText:{type:String,required:true},
  userReportedList:[reportedUsers],
  userAnsweredList:[userAnswered],
  correctAnswere:{type:String,required:true},
});


via Anil Sharma

No comments:

Post a Comment