我是mean.js的新手所以它可能只是错误的语法,但是当我使用带有查询的model.find()时(option.find({poll_id:1})它返回一个空数组。
poll.server.model.js
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Poll Schema
*/
var PollSchema = new Schema({
poll_id: {type:Number},
user_id: {type:Number},
poll_question: {type:String},
poll_language: {type:String},
poll_description: {type:String},
poll_description_raw: {type:String},
poll_weight_additional: {type:Number},
poll_flag_active:{type:Number,default:1},
poll_flag_18plus:{type:Number,default:0},
poll_flag_expire:{type:Number,default:0},
poll_flag_deleted:{type:Number,default:0},
poll_flag_moderated:{type:Number,default:0},
poll_flag_favourised:{type:Number,default:0},
poll_date_expiration:{type:Date},
poll_date_inserted:{type:Date,default:Date.now()},
poll_flag_updated:{type:Date},
show_thumbs:{type:Number},
comments:{
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Poll', PollSchema);
option.server.model.js
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Option Schema
*/
var OptionSchema = new Schema({
option_id:{type:Number},
poll_id:{type:Number},
option:{type:Number}
});
mongoose.model('Option', OptionSchema);
polls.server.controller.js
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors.server.controller'),
Poll = mongoose.model('Poll'),
Option = mongoose.model('Option'),
_ = require('lodash');
/**
* List of Polls
*/
exports.list = function(req, res) {
Poll.find().limit(10).sort('_id')/*.populate('user', 'displayName')*/.exec(function(err, polls) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
for (var i=0; i < polls.length; i++) {
// this works, returns an array with 10 items
Option.find().exec(function(err,docs){
console.log(docs);
});
// this doesnt! although i see in the former array that there are options
// with the poll_id set to 1.
Option.find({'poll_id':1}).exec(function(err,docs){
console.log(docs);
});
}
res.json(polls);
}
});
};
我在做错了什么?我查了一下,但我没有看到任何帖子提到我的问题。
我尝试使用model.find()。where('poll_id')。equals(1)有和没有引号,但没有。当我运行model.find()它工作,但无论我如何尝试过滤它,它返回一个空数组。感谢adcvance!
答案 0 :(得分:0)
我知道你是从MySQL导入的,但使用Mongoose存储和搜索你的数据的正确方式将是人口和参考:
var Polls = mongoose.Schema({
options: [{ type: Schema.Types.ObjectId, ref: 'Options' }]
});
var Options = mongoose.Schema({
...
});
Polls
.find()
.limit(10)
.sort('_id')
.populate('options')
.exec(function (err, polls) {
console.log(polls.options); // => [ array of options ]
});
可在此处找到相关文档http://mongoosejs.com/docs/populate.html
我高度建议花一些时间重构您的数据,这样您就可以使用population和refs的许多优点(索引,清理代码等)。不要试图在文档存储上强制使用关系范例。完全撕掉乐队助手或坚持你所得到的知识。
也就是说,你可以用你当前的数据来实现你想要的东西:
// npm install --save async
var async = require('async');
exports.list = function(req, res, next) {
Poll.find().limit(10).sort('_id').exec(function (err, polls) {
// In true HTTP parlance, the correct thing to do here would
// be to respond with a 500 instead of a 400. The error would
// be coming from MongoDB, not a poorly formed request by the
// client. Remember: 40Xs are for client errors and 50Xs are
// for server errors. If this is express, you should also look
// into using a middleware error handler and passing the error
// down the line like so:
if (err) return next(err);
// Since you used a return statement above, there is no need for
// the if/else but this is a matter of style. If it helps with the
// readability of your code, keep it. I choose to eliminate because
// async code has enough nesting as it is!
// https://github.com/caolan/async#maparr-iterator-callback
async.map(polls, function (poll, callback) {
// Now look up your option by its poll_id
Option.find({'poll_id': poll.poll_id}, function (err, options) {
if (err) return callback(err);
poll.options = options;
callback(null, poll);
});
}, function (err, data) {
// Again, this would be a Mongo error, so pass it down the line to
// handle as a generic 500.
if (err) return next(err);
// The data parameter is your newly mapped polls array
res.json(data);
});
});
};
注意在没有人口和参考的情况下实现了多少样板文件。此外,在封面下,人口只会在数据库中击中两次,而在这里,你要击中它11次。从长远来看,您现在可以更好地更改数据,然后生成关系/文档存储混合体。我见过他们而且他们不漂亮;)