简体   繁体   English

猫鼬发现无法使用默认属性

[英]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 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. 好的,我将state一些值从“ initial”更改为“ pending”,而mongo找到了“ pending”,但是,它无法识别“ initial”,并返回了0个结果。

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 我已经在计算机上运行了相同的代码,并且实际上它找到了具有初始状态的工作,我使用在计算机中运行的代码在名为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: 您需要启动并运行mongodb服务器,然后安装以下依赖项:

$ npm install mongoose async

then run the file: 然后运行文件:

$ node app.js

Let me know if it works this code for you. 让我知道该代码是否对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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