简体   繁体   中英

Mongoose find not working on default attribute

This is probably really simple, but I can't see what I'm missing.

Here is my model:

'use strict';

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var JobSchema = new Schema({
  type: String,
  scheduled: { type:Boolean, default:false },
  state: { type: String, default: 'initial' },
  details: {},
  changes: [],
  lastModified: { type: Date, default: Date.now },
});

//RECCOMMENDED TO TURN OFF IN PRODUCTION?
JobSchema.index({type:1, state:1, "details.prodId":1});

module.exports = mongoose.model('Job', JobSchema);

However, whenever I run the following code nothing is returned:

Job.find({state:'initial'},function(err,data){
    console.log(data.length);
});

I've tested this same code on other attributes, works.

Also here is some example db data:

[
  {
    "_id":"55709ed07df3b4b1f034311a",
    "details": {
       "quantity":"10",
       "price":"45.28",
       "prodId":"321product",
       "storeId":"338store"
    },
    "lastModified":"2015-06-06T18:00:28.348Z",
    "changes":[],
    "state":"initial",
    "scheduled":true
  },...

UPDATE

Ok I changed some values of state from 'initial' to 'pending' and mongo finds the 'pending', however, it doesn't recognize 'initial', and returns 0 results.

Probably your querying to a database that is not supposed to be used.

I have run the same code on my computer and actually it finds a job with initial state, I have created a gist with the code that I run in my computer in a file called app.js : https://gist.github.com/wilsonbalderrama/653c34acf19871115f64

You need to have your mongodb server up and running and then please install the dependencies that are:

$ npm install mongoose async

then run the file:

$ node app.js

Let me know if it works this code for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM