查询出来是一堆没用的东西呢,为啥不是文档呢:
//db.js var mongoose = require(‘mongoose’); mongoose.connect(‘mongodb://localhost/mybooks’); module.exports = mongoose;
//find.js var mongoose = require(‘./db’); var Schema = mongoose.Schema; var booksSchema = new Schema({ name: String, author: String });
var bookModel = mongoose.model('ctbook’, booksSchema); console.log(bookModel.find());
{ _mongooseOptions: {}, mongooseCollection: { collection: null, opts: { bufferCommands: true, capped: false }, name: 'ctbooks’, conn: { base: [Object], collections: [Object], models: {}, replica: false, hosts: null, host: 'localhost’, port: 27017, user: undefined, pass: undefined, name: 'mybooks’, options: [Object], otherDbs: [], _readyState: 2, _closeCalled: false, _hasOpened: false, _listening: false, _events: {}, db: [Object] }, queue: [], buffer: true }, model: { [Function: model] base: { connections: [Object], plugins: [], models: [Object], modelSchemas: [Object], options: [Object] }, modelName: 'ctbook’, model: [Function: model], db: { base: [Object], collections: [Object], models: {}, replica: false, hosts: null, host: 'localhost’, port: 27017, user: undefined, pass: undefined, name: 'mybooks’, options: [Object], otherDbs: [], _readyState: 2, _closeCalled: false, _hasOpened: false, _listening: false, _events: {}, db: [Object] }, discriminators: undefined, schema: { paths: [Object], subpaths: {}, virtuals: [Object], nested: {}, inherits: {}, callQueue: [], _indexes: [], methods: {}, statics: {}, tree: [Object], _requiredpaths: undefined, discriminatorMapping: undefined, _indexedpaths: undefined, options: [Object], _events: {} }, options: undefined, collection: { collection: null, opts: [Object], name: 'ctbooks’, conn: [Object], queue: [], buffer: true } }, op: 'find’, options: {}, _conditions: {}, _fields: undefined, _update: undefined, _path: undefined, _distinct: undefined, _collection: { collection: { collection: null, opts: [Object], name: 'ctbooks’, conn: [Object], queue: [], buffer: true } }, _castError: null }
直接在用mongo能查到吗? 如果可以的话。那就是你mongoose用的有问题。 我是这样用的
var meetingSchema = new Schema({
id:Schema.Types.ObjectId,
meetingRoomName: String,
meetingId:String,
meetingSubject:String,
members: Schema.Types.Mixed,
updated: { type: Date, default: Date.now },
startTime:{ type: Date, default: Date.now },
ownerCorpId:String,
ownerName:String
});
var meetingModel = mongoose.model('meetings’, meetingSchema);
var query2 = meetingModel.find(conditions);
query1.exec();
@xuwenfei886 少点东西:
var mongoose = require(‘./db’),
_ = require(“underscore”),
Schema = mongoose.Schema;
db.js:
var mongoose = require(‘mongoose’).connect(‘mongodb://localhost/Titan’);
module.exports = mongoose;
@jaycreater -.-mongoose执行find是异步的…返回值是mongoose本身对象 也可以说在nodejs中大多数都是方法都是异步执行的
bookModel.find(function(err, docs) {
// err是错误信息,docs就是查询返回的文档,是一个数组
console.log(docs);
});
@jaycreater 以express中为例
bookModel.find(function(err, docs) {
if(err) {
console.error(err);
return;
}
res.render('view', {
list: docs
});
});