5

In the 'QAns' model, I want to load all the documents in which the 'Answers' field is an empty array using mongoose in node.js. I am using MongoDB Atlas for storing my data. This is my schema for the database:

const QuestAnsSchema = new Schema({
    text: String,
    author: String,
    answers: [{
        text: String,
        author: String
   }]
});

This is the data in the MongoDB database.

{
     "_id":{"$oid":"5ee1f235a1e9870daca7d5e9"},
     "text":"Question 1",
     "author":"5ee1b8ebdbf91b23a808d417",
     "answers":[],
     "__v":{"$numberInt":"0"}
},
{
     "_id":{"$oid":"5ee1f235885770darrr7f449"},
     "text":"Question 2",
     "author":"5ee1b8ebdbf9o2w3a808d417",
     "answers":[],
     "__v":{"$numberInt":"0"}
}

Both the documents have the 'answers' field empty in them but assuming there would be some documents with a non-empty 'answers' field how will I load the ones with no answers field?

I have tried this code but it gives a 'Bad Request' error:

router.get('/questions', (req, res) => {
    QAns.find({answers: { $exists: true, $ne: [] } }).then(( err, result) => {
        if(err)  return res.sendStatus(400);

        res.render('questions',{ result });

        console.log(result);
    });

})
  • QAns is the model name, answers is the array field
  • 'questions' is the ejs file which will display those documents after they are loaded.

2 Answers 2

6

You can use $size operator to fetch all the documents where answers array is of length zero.

QAns.find({
  answers: { $size: 0 } 
})

$size operator is used to query an array by number of elements.

Edit:

To select all the documents where answers array is not empty, use $not operator along with $size operator.

Following query will select all the documents where answers array is not empty

QAns.find({
  answers: {
    $not: { $size: 0 }
  }
})

$not operator performs a logical NOT operation on the specified <operator-expression> and selects the documents that do not match the <operator-expression>

P.S. Above query will also return those documents where answers field is not present

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

2 Comments

Thanks, it worked! How do I do this for a non-empty answers field?
you mean you want to find those documents where answers array is not empty? If yes then see the updated answer
0

$exists works great for your needs, you just have a syntax error in your query coming from your $ne.

It's unclear if you want documents with answers or without so ill provide both options:

  1. this will provide documents without the answer field:
QAns.find({answers: { $exists: false }) 
  1. this will provice documents with at least one answer in the answers field:
QAns.find({"answers.0": { $exists: true } })
  1. this will provide documents with answers=[]
QAns.find({"optOuts.0": {$exists: false}, optOuts: {$exists: true}})

Or as Yousaf suggjested:

QAns.find({"answers": { $size: 0 } })

3 Comments

OP wants to fetch documents which have empty answers array, not those documents where answers field doesn't exists or those where there's atleast one item in answers array.
I'm not so sure you're right as the explanation is poorly worded and the code snipper he posted has $ne: [].
I want to load all the documents in which the 'Answers' field is an empty array

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.