vpmprojects
vpmprojects

Reputation: 3

Using Mongoose to access values in an array

I am trying to write a project app that monitors medical data implemented using a MEAN stack. I intend to simulate the data by sending a 'vitalsTick' about every second. These are stored as an array of subdocuments in the patient document.

var mongoose = require('mongoose');

var vitalsTickSchema = new mongoose.Schema({
  time : {type: Number, min : -299, max: 300, required: true},
  pulse : {type: Number, required:false},
  rhythm : {type: String, required: false},
  resp : {type: Number, required: false},
  spo2 : {type: Number, required: true}
});

var VitalsTick = mongoose.model('vitalsTick', vitalsTickSchema, 'vitalsTick');

module.exports = VitalsTick;

and

var vitalsTickSchema = mongoose.model('vitalsTick').schema;

var patientRecordSchema = new mongoose.Schema({
  name : {type: String, required: true},
  mrn : {type: Number, required: true},
  dob : {type: Date, required: false},
  address : {type: String, required: false},
  mhx : {type: [{history : {type: String, required: false}}]},
  vitalTicks : [vitalsTickSchema]
});

var PatientRecord = mongoose.model('patientrecord', patientRecordSchema, 'patients');

module.exports = PatientRecord;

I have written some python to generate test data in json that validates using jsonlint and then imported to mongodb. Before moving to the next step of development, I want to ensure the schemas work as planned. Using Mongo:

> db.patients.find()
{ "_id" : ObjectId("59c2fc69b9e18eb6ad18c063"), "name" : "Testie 
McTestface", "mrn" : "11111111", "dob" : "", "address" : "", "mhx" : [ {  } ],
"vitalTicks" : [ { "time" : 0, "pulse" : 75, "rhythm" : "sinus",
"rr" : 20, "SpO2" : 96 }, ... ] }  

My problem is this:

> db.patients.find({vitalTicks: {time : {$eq :0}}},{'vitalTicks.$':1})
> 

As far as I can tell should return

{ "_id" : ObjectId("59c2fc69b9e18eb6ad18c063"), "vitalTicks" : [ { 
"time" : 0, "pulse" : 75, "rhythm" : "sinus",
"rr" : 20, "SpO2" : 96 } ] }

But it returns nothing.

Cheers.

Upvotes: 0

Views: 808

Answers (1)

Euclides
Euclides

Reputation: 415

No, actually it is an array of embedded documents, the following query makes the job:

db.patients.find({"vitalTicks.time" : 0}, {"vitalTicks.$":1})

Hope this helps.

Upvotes: 1

Related Questions