Results from MongoDB

532 Views Asked by At

I'm running into two issues with Derby and Mongo. I come from a MySQL and Redis background so maybe I'm just doing something wrong?

1) It seems I can't search for an Id whose type is ObjectId.

model.query("users").byKey(params.userId);

Will only match records whose _id is a string.. Which I guess would be okay, except when the data is "updated" Racer will create a new record with an ObjectId _id! So basically I can't ever retrieve a document by id that I've inserted with Racer!

2) Every time I do a query I get back an object of objects such as:

{ '$spec': true, 
    '4fcd4c8e6c8c89d97ed90f4a': { "username": ... },
    '4fcd4c8e6c8c89d97ed90f4b': { "username": ... },

Which means I have to convert it into a list of objects. I wrote a function that creates a list of the _id's, then assigns it to a model variable for use with refList. It feels like such a hack, this can't be the right way to do it.

2

There are 2 best solutions below

0
On BEST ANSWER

Queries were just substantially updated in version 0.3.11. Models now have built-in model.filter() and model.sort() methods which will replace the need to manually build a list of keys and emit much more efficient array update events to Derby.

Please see the Queries README.

3
On

I'm not a derbyjs user, however here are some suggestions regarding your problems:

1) Regarding "Id is ObjectId"?

It seems that id have to be an object, see DerbyDoc/Persistance:

Racer paths are translated into database collections and documents using a natural mapping: collection.documentId.document

...

// The first and second segments in root paths must be objects

How are you saving your data? As a general advice, a full reproducible code would improve the help you can get.

2) "I need to convert the query result to list"?

From my experience with other engines, it indeed seems you're not using the right approach. All will depend on what you want to do with the result, but one very common approach is to work with object list and finally get attributes in a template loop. In your case it will mean pass directly the query result to the page.render call. See DerbyDoc/sections

Hope this helps

Alexis