0

Background Information

I've been following this tutorial: http://adrianmejia.com/blog/2014/10/01/creating-a-restful-api-tutorial-with-nodejs-and-mongodb/#mongoose-read-and-query

I have a mongodb called test and it has the following collections:

> show collections
chassis
ports
customers
locations
system.indexes
> 

Symptoms

When I try to query for any document inside the chassis collection, it keeps returning null even though many records exist.

 dev@devbox:~/nimble_express$ curl localhost:3000/chassis/55a7cc4193819c033d4d75c9
 nulldev@devbox:~/nimble_express$ 

Problem

After trying many different things, I discovered the following issue in the mongodb logs ( i turned on verbose logging)

In the following log entry, notice the reference to "test.chasses" (which is a typo. It should be "chassis") :

2015-07-29T14:42:25.554-0500 I QUERY    [conn141] query test.chasses query: { _id: ObjectId('55a7cc4193819c033d4d75c9') } planSummary: EOF ntoskip:0 nscanned:0 nscannedObjects:0 keyUpdates:0 writeConflicts:0 numYields:0 nreturned:0 reslen:20 locks:{ Global: { acquireCount: { r: 2 } }, MMAPV1Journal: { acquireCount: { r: 1 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { R: 1 } } } 0ms

I've grepped to make sure I don't have this typo anywhere in my code using the following command:

dev@devbox:~/nimble_express/nimbleApp$ grep -ris 'chasses' .
dev@devbox:~/nimble_express/nimbleApp$ 

I'm not sure where it's getting this collection name from.

Other queries against other collections work just fine. For example, I have a collection called "ports" and I pretty much copied and pasted all logic I have for chassis' and it works just fine.

Here's the proof from the logs:

2015-07-29T14:58:15.127-0500 I QUERY    [conn160] query test.ports planSummary: COLLSCAN cursorid:68808242412 ntoreturn:1000 ntoskip:0 nscanned:0 nscannedObjects:1000 keyUpdates:0 writeConflicts:0 numYields:7 nreturned:1000 reslen:188922 locks:{ Global: { acquireCount: { r: 16 } }, MMAPV1Journal: { acquireCount: { r: 8 } }, Database: { acquireCount: { r: 8 } }, Collection: { acquireCount: { R: 8 } } } 0ms

Any suggestions? I'm sure I have a typo somewhere... but I can't find it. All my code is within the nimble_express directory tree.

dot
  • 14,928
  • 41
  • 110
  • 218

1 Answers1

0

I copied the 'chassis' collection in my mongodb to "tempCollection" like this:

> db.createCollection('tempCollection')
{ "ok" : 1 }
> db.chassis.copyTo('tempCollection');
WARNING: db.eval is deprecated
57
> exit
bye

And then, I created my schema, a route for this collection. When I attempted to do a curl request for localhost:3000/tempCollection, I noticed that in the logs, the name of the collection was wrong again.

[[A2015-07-29T15:13:19.661-0500 I QUERY    [conn168] query test.tempcollections planSummary: EOF ntoreturn:1000 ntoskip:0 nscanned:0 nscannedObjects:0 keyUpdates:0 writeConflicts:0 numYields:0 nreturned:0 reslen:20 locks:{ Global: { acquireCount: { r: 2 } }, MMAPV1Journal: { acquireCount: { r: 1 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { R: 1 } } } 0ms

And that's when it dawned on me. Something somewhere was pluralizing the collection names! So I googled and found this post:

Is there a way to prevent MongoDB adding plural form to collection names?

So the solution for me was to explicitly define the collection name like so:

 module.exports = mongoose.model('chassis', ChassisSchema, 'chassis');

inside the model/Chassis.js file

Instead of marking this as a duplicate, I think I should leave this question as is for those who think they have a problem with collection names. For noobs like me, you assume that you are doing something wrong vs. the system performing some automagic for you! But I'm happy to do whatever the community suggests!

We can close this off as a duplicate. Or leave as is.

Community
  • 1
  • 1
dot
  • 14,928
  • 41
  • 110
  • 218