0
var LINK  = mongoose.model('link', {
  id: mongoose.Schema.Types.ObjectId,
  linkA: Boolean,
  linkB: Boolean,

}, 'link')

(model for LINK - here as reference)


When using mongoose on nodeJS I can use the find() function to return all documents in a collection:

var Test = LINK.find({}, (err, user) => {
  if (err) console.log("error: " + err)
  else  console.log("users: " + user)
}, 4000)

which returns all the data in terminal. Easy, right?

But the problems arise when I try to use a query:

var Test = LINK.find({linkA:'true'}, (err, user) => {
  if (err) console.log("error: " + err)
  else  console.log("users: " + user)
}, 4000)

The query at first didn't return any results (even though there were documents on mongodb populated beforehand). But after adding documents to mongodb via mongoose- the documents added can be accessed, but not any of the others that were created on the mongodb console.

Is there a reason for this? I'm suspicious that I'm using the find() function wrong but it seems to work fine when the documents were added via mongoose or I use an empty query so I'm not sure.

Any help would be appreciated. Thanks.

Izaac Barratt
  • 11
  • 1
  • 3
  • Are you using linkA:'true' in find as a string on purpose? – Talha Awan Oct 29 '16 at 23:13
  • Yes- I've tested with and without quotes and it works the same. – Izaac Barratt Oct 29 '16 at 23:27
  • 1
    Possible dupe of https://stackoverflow.com/questions/14183611/mongoose-always-returning-an-empty-array-nodejs. Make sure you understand that it's using the `links` collection, not `link`. – JohnnyHK Oct 30 '16 at 04:11
  • No it's not that. The query works when it's empty and returns all the data, it's only when trying to use a query with parameters I can't access the data unless it was added via mongoose. – Izaac Barratt Oct 30 '16 at 10:52
  • I think the 'true' is the problem. The property is a boolean. The fact that it is working in mongoose means that mongoose is doing some conversion for you. – Amiram Korach Oct 30 '16 at 11:42
  • Yes, you were right! Thank you. I'm trying to get used to noSQL and sometimes the lack of restrictions are overwhelming. Must have missed it. – Izaac Barratt Oct 30 '16 at 12:11

1 Answers1

0

The problem was the way I was storing the information.

As pointed out by Amiram Korach the documents had stored boolean values with quotation marks by mistake and so mongoose was recognising it as a string value.

Izaac Barratt
  • 11
  • 1
  • 3