user3669481 user3669481 - 2 months ago 8
JSON Question

Using existing fields as objectId in mongodb to create REST api with node and express?

My JSON file is:

{'name': name,
'birthday': birthday,
'telephone': telephone,
'details': details},

My application will be able to accept
values, and these three values combined are going to be unique for each entity. I am hoping to retrieve an entity based on these three values, say http://localhost:8000/name?=mary&birthday?=19880902&telephone?=2234324567 will return the json object of Mary.

I am looking at the examples available on using mongodb and nodejs, most of them suggest creating a completely new
field using
new BSON.ObjectID(id)
and do something similar to
app.get('/users/:id', users.findById)
to find the id. Is it possible not to create a separate id field?


Try using a callback with the (req, res) parameters. See here:

app.get('/', function(req, res) {
  var findCursor = Users.find({ 
    "birthday": req.query.birthday,  
    "telephone": req.query.telephone 
  cursor.each(function(err, doc) {
    if (err) console.log("Oh no!");
    if (doc != null) {
      // Do stuff
      res.send('GET request to user page?');