redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

Proper way to query list of id's on mongoDB with redstone_mapper_mongo? #62

Closed cgarciae closed 9 years ago

cgarciae commented 9 years ago

In the example in the docs you use

@Id()
String id;

to interact with mongo's Id Object. However, it is common for you have a list of ids of objects in another collection. How do I make that query and/or structure my class?

luizmineo commented 9 years ago

You can use the @ReferenceId annotation (it isn't documented yet, but I hope to fix that soon)

@ReferenceId
List<String> ids;
luizmineo commented 9 years ago

See http://www.dartdocs.org/documentation/redstone_mapper_mongo/0.1.2/index.html#redstone_mapper_mongo/mongodb_metadata.ReferenceId

cgarciae commented 9 years ago

@luizmineo Thanks man! You always save me.

I've been depending on your code a lot and I would like to contribute back atleast with documentation. Tell me if you need anything.

cgarciae commented 9 years ago

Ok, so now I have a list of Strings that are the ids of some objects in the DB. How do I use this list to FIND those objects?

luizmineo commented 9 years ago

That depends on how you are building your query. For example, if you want to retrieve all objects with a single query, you can do the following:

var selector = where.oneFrom("_id", myOuterObj.ids.map((i) 
    => new ObjectId.fromHexString(i)).toList());

mongoDb.find("mycollection", MyInnerType, selector);

Let me know if that helps

cgarciae commented 9 years ago

Thanks! Id been looking for $in but now I see it's a reserved word so they had to come up with oneFrom.