1

I am using MongoDB with Nodejs and I am trying to find the usertype value of a specific user so I've got this:

var myQuery= { username: req.body.username }, { "usertype": 1 };
 dbo.collection("usercollection").find(myQuery, function(err, obj) { ...

What I need returned is the value of usertype from that specific user but it keeps saying I have a syntax error with : expected.

How can I fix this?

2 Answers2

2

You have a syntax error since you're trying to define query and projection for find method in one line, try:

var myQuery= { username: req.body.username };
var options = { projection: { usertype: 1 } };
var cursor = dbo.collection("usercollection").find(myQuery, options)
mickl
  • 48,568
  • 9
  • 60
  • 89
  • It's returning 1 on usertype ... it's a string ... I don't expect usertype to be 1 ... I just need to get the value of whatever usertype value is ... I might have not explain myself properly. –  Apr 04 '19 at 14:43
  • If you use node.js driver for Mongo then you need to add `projection` as a top level property, check my modified answer – mickl Apr 04 '19 at 14:49
  • Tried it but same issue console.log(obj); returns a lot of crap and no db fields info –  Apr 04 '19 at 14:57
  • @monroe2019 I think the reason is that you're dealing with cursors in node.js driver. You can take a look here https://stackoverflow.com/questions/25507866/how-can-i-use-a-cursor-foreach-in-mongodb-using-node-js to check how to work with them – mickl Apr 04 '19 at 15:00
1

I recommends to first perform the find operation to get the cursor and after it extract data from the cursor like below,

var myQuery= { username: req.body.username };
dbo.collection("usercollection").find(myQuery, {  projection: { usertype: 1}  }).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
  });

In your case, the above code will work fine.

Arsal Imam
  • 2,882
  • 2
  • 24
  • 35
  • also, find's 2nd param requires a separate object of projection, please check in the above code! – Arsal Imam Apr 04 '19 at 14:38
  • It's returning 1 on usertype ... it's a string ... I don't expect usertype to be 1 ... I just need to get the value of whatever usertype value is ... I might have not explain myself properly. –  Apr 04 '19 at 14:43
  • remove projection and check whether it is returning the complete user object! – Arsal Imam Apr 04 '19 at 14:48
  • I've tried console.log(obj); and it's returning way too much info back. –  Apr 04 '19 at 14:51
  • and it is also containing the user type? – Arsal Imam Apr 04 '19 at 14:52
  • also, as of the above code, you should console.log(result) instead of body! – Arsal Imam Apr 04 '19 at 14:53
  • No, just a lot of other information: eg: raw: undefined, [1] hint: null, [1] timeout: undefined, [1] slaveOk: false, [1] readPreference: null }, [1] options: [1] { skip: 0, [1] limit: 0, [1] raw: undefined, [1] hint: null, [1] timeout: undefined, [1] slaveOk: false, [1] readPreference: null, [1] db: [1] Db { [1] _events: {}, [1] _eventsCount: 0, [1] _maxListeners: undefined, [1] s: [Object], [1] serverConfig: [Getter], [1] bufferMaxEntries: [Getter], –  Apr 04 '19 at 14:54
  • this is the cursor data, did you use the above code `toArray`? – Arsal Imam Apr 04 '19 at 14:56