admin管理员组文章数量:1244213
I am trying to create a simple MongooseJS example program that gets a list of items from a collection, and it's ing back empty every time. Here is the code:
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var sampleSchema = new Schema({
sampleField : String
});
var db = mongoose.connect('mongodb://localhost:27017/test');
var sampleCollection = mongoose.model('sampleCollection', sampleSchema);
sampleCollection.find({ } , function (err, items) {
console.log(items); // outputs []
console.log(err); // outputs null
items.forEach( function(item) {
console.log(item); // does not reach this code
});
});
I have a default instance of MongoDB running, and this is what I've entered in the shell:
> use test
> db.sampleCollection.save({sampleField : "Hello"});
> db.sampleCollection.save({sampleField : "Goodbye"});
> db.sampleCollection.find({});
{ "_id" : ObjectId("4f28944b38b59225012109da"), "sampleField" : "Hello" }
{ "_id" : ObjectId("4f28945138b59225012109db"), "sampleField" : "Goodbye" }
Any idea why my code doesn't return any data?
Thanks for your help, Dave
I am trying to create a simple MongooseJS example program that gets a list of items from a collection, and it's ing back empty every time. Here is the code:
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var sampleSchema = new Schema({
sampleField : String
});
var db = mongoose.connect('mongodb://localhost:27017/test');
var sampleCollection = mongoose.model('sampleCollection', sampleSchema);
sampleCollection.find({ } , function (err, items) {
console.log(items); // outputs []
console.log(err); // outputs null
items.forEach( function(item) {
console.log(item); // does not reach this code
});
});
I have a default instance of MongoDB running, and this is what I've entered in the shell:
> use test
> db.sampleCollection.save({sampleField : "Hello"});
> db.sampleCollection.save({sampleField : "Goodbye"});
> db.sampleCollection.find({});
{ "_id" : ObjectId("4f28944b38b59225012109da"), "sampleField" : "Hello" }
{ "_id" : ObjectId("4f28945138b59225012109db"), "sampleField" : "Goodbye" }
Any idea why my code doesn't return any data?
Thanks for your help, Dave
Share Improve this question asked Feb 1, 2012 at 1:27 Dave MorrisDave Morris 8569 silver badges26 bronze badges2 Answers
Reset to default 13mongoose
will normalize the name of collection to lowercase and pluralzed. Therefore, you should insert into db.samplecollections
instead of db.sampleCollection
. (Notice the difference of letter c
and s
here).
to test it:
s = new sampleCollection({sampleField: 'hello'}); // creates a new record
s.save(function(err) {
sampleCollection.find({ } , function (err, items) {
console.log(items);
console.log(err);
items.forEach( function(item) {
console.log(item);
});
});
});
and it properly prints:
[ { sampleField: 'hello', _id: 4f28ab4cc9e58f710a000001 } ]
null
{ sampleField: 'hello', _id: 4f28ab4cc9e58f710a000001 }
then in mongo shell:
> show collections
samplecollections //<<<<<<<<<<<<<< It's all lowercase and pluralized
system.indexes
> db.samplecollections.find()
{ "sampleField" : "hello", "_id" : ObjectId("4f28ab4cc9e58f710a000001") }
While this is true, you can specify the name of the collection in the third argument and it will use the case from that string:
var sampleCollection = mongoose.model('sampleCollection', sampleSchema,'SampleCollection');
本文标签: javascriptMongoose JS queries all coming back null or emptyStack Overflow
版权声明:本文标题:javascript - Mongoose JS queries all coming back null or empty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740213774a2242489.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论