Help getting started

121 views
Skip to first unread message

Jonas

unread,
Feb 24, 2011, 1:55:36 AM2/24/11
to Mongoose Node.JS ORM
For some reason I can't get this working. I've spent the night
looking through this Google group, reading the api, and examples. I
have a pretty simple case, but I can't get any data back when I
query. I have a Mongo db called testdb and 1 collection called
userSessions. From the mongo command line:

> db.userSessions.find({});
{ "_id" : ObjectId("4d65e4fe68eb3ee95926ae05"), "userId" : 1,
"sessionIds" : [ "0317cd74-78f9-4ad5-96da-798be526cb39" ] }
> show collections
system.indexes
userSessions


Here's my mongoose code that I can't get to work:

var mongoose = require('mongoose');

var userSessionsSchema = new mongoose.Schema({
userId: Number,
sessionIds: Array
});

mongoose.connect('mongodb://localhost/testdb');
mongoose.model("userSessions", userSessionsSchema);
var userSessionsModel = mongoose.model("userSessions");
userSessionsModel.find({}, function(err, doc) {
console.log(err);
console.log(doc);
});

The err console log outputs null and the doc console log outputs [].

Guillermo Rauch

unread,
Feb 24, 2011, 3:18:38 AM2/24/11
to mongoo...@googlegroups.com
Mongoose probably thinks your collection is `usersessions` (lowercase). Try passing a second argument to `mongoose.model` with the collection name (userSessions)
--
Guillermo Rauch
http://devthought.com

Jonas

unread,
Feb 24, 2011, 11:53:42 AM2/24/11
to Mongoose Node.JS ORM
Yep that was it. Thanks!

doubleTap

unread,
Mar 12, 2011, 5:02:08 PM3/12/11
to Mongoose Node.JS ORM
Wow! What?

I had the same problem and that fixed it but I do not see this in the
documentation. Can you please point me to the documentation on this?

Thank you,

-doubletap

Sander Pick

unread,
Mar 12, 2011, 7:45:27 PM3/12/11
to mongoo...@googlegroups.com
should I be able to do this?

Media.find({ 'meta.tags': 'one' }, function (err, media) {
console.log(err.stack);
});

it yields:

TypeError: Cannot read property 'prototype' of undefined
at SchemaArray.castForQuery (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/schema/array.js:127:25)
at Query.cast (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/query.js:176:32)
at Query.execFind (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/query.js:557:10)
at Query.find (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/query.js:91:15)
at Function.find (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/model.js:376:16)
at findMedia (/Users/sander/Sites/island/app.js:120:9)
at Object.<anonymous> (/Users/sander/Sites/island/app.js:220:31)
at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)
at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:157:15)
at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:157:15)

running it in the mongo shell works as expected:

db.medias.find({ 'meta.tags': 'one' })

yields all docs with a tag of 'one' in the meta.tags array.

any ideas?

thank you,
sander

DoomedLung

unread,
Mar 14, 2011, 11:22:44 AM3/14/11
to Mongoose Node.JS ORM
I'm getting a similar issue, where an empty array is logged...

Code:-
var mongoose = require('mongoose')
,Schema = mongoose.Schema;

var compSchema = new Schema({
'name': String
});
mongoose.connect('mongodb://127.0.0.1/components'); //use of ip on
windows only works, instead of 'localhost'
mongoose.model('compModel', compSchema);
var compModel = mongoose.model('compModel');
compModel.find({}, function(err, c){
console.log(c);
});

Displays:-
[ ]

I tried the second argument solution but haven't a clue if I'm doing
it right, I tried:-
var compModel = mongoose.model('compModel', compModel);


Any ideas?
Cheers

On Mar 13, 12:45 am, Sander Pick <sanderp...@gmail.com> wrote:
> should I be able to do this?
>
> Media.find({ 'meta.tags': 'one' }, function (err, media) {
>   console.log(err.stack);
>
> });
>
> it yields:
>
> TypeError: Cannot read property 'prototype' of undefined
>     at SchemaArray.castForQuery (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/schema/array. js:127:25)
>     at Query.cast (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/query.js:176: 32)
>     at Query.execFind (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/query.js:557: 10)
>     at Query.find (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/query.js:91:1 5)
>     at Function.find (/usr/local/lib/node/.npm/mongoose/1.1.4/package/lib/mongoose/model.js:376: 16)
>     at findMedia (/Users/sander/Sites/island/app.js:120:9)
>     at Object.<anonymous> (/Users/sander/Sites/island/app.js:220:31)
>     at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/rou ter.js:146:21)
>     at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/rou ter.js:157:15)
>     at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/rou ter.js:157:15)

Sander Pick

unread,
Mar 17, 2011, 6:38:36 PM3/17/11
to mongoo...@googlegroups.com
took another look and realized i needed the $in key.

i = jackass

thanks, bye.

Camilo Aguilar

unread,
Mar 18, 2011, 12:27:11 PM3/18/11
to mongoo...@googlegroups.com, Sander Pick
what was the final result? I ask because that will be useful for people having the same issue.

Sander Pick

unread,
Mar 18, 2011, 3:58:24 PM3/18/11
to mongoo...@googlegroups.com
cool, so... here is a schema i'm using:

Media = new Schema({
      key             : { type: String, index: true }
    , title             : String
    , terms         : { type: Array, index: true }
    , body          : String
    , comments : [Comment]
    , type            : String
    , added        : { type: Date, default: Date.now }
    , meta           : {
          tags        : { type: Array, index: true }
        , hits         : { type: Number, default: 0 }
        , likes       : { type: Number, default: 0 }
      }
    , member_id   : ObjectId
    , attached    : {}
  });
mongoose.model('Media', Media);

(different scope)
Media = mongoose.model('Media');

so to find all media with a tag of 'posters', you would do:

Media.find({ 'meta.tags': { $in: ['poster'] } }, function (err, media) {});

In my case, I've got the title of a doc parsed into searchable terms... so i can search over that field as well:

Media.find({ $or: [{ 'meta.tags': { $in: ['posters'] } }, { terms: { $in: ['posters'] } } ] }, function (err, media) {});

Seems to work just like http://www.mongodb.org/display/DOCS/Advanced+Queries, haven't tried all the operators though.

-s
Reply all
Reply to author
Forward
0 new messages