Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Generate list of subdocuments #3

Closed
stemuk opened this issue Jul 5, 2018 · 2 comments
Closed

Generate list of subdocuments #3

stemuk opened this issue Jul 5, 2018 · 2 comments

Comments

@stemuk
Copy link

stemuk commented Jul 5, 2018

This isn't really an issue, just me having a problem and no better place to ask the question :)

I am currently implementing objectdb and created this test document:

              {
                'cards': {'key01': {'question': 'buscar', 'answer': 'search'},'key02': {'question': 'tener', 'answer': 'have'} },
                'name': 'Spanish Vocabs',
              },

As you can see I tried to create a document with a subdocument named cards, that again has subdocuments that will have a randomly generated key ('key01' etc. in this example) as an identifier.

Now I would like to put all the subdocuments of cards into a List [ ] that I could pass on to another screen of the app. Is this possible with objectdb and if so, could you give me a hint on how to implement it?

@marioreggiori
Copy link
Owner

I am not sure if I understand your question correctly.
Projection like in the MongoDB API isn't possible yet.
I haven't actually tested the following code.
But maybe you can use some of it.

var results = await db.find({'some':'filter'});

var cards = [];

for(var result in results){
  if(result.containsKey('cards') && result['cards'] is Map){
    for(var key in result['cards'].keys){
      cards.add(result['cards'][key]);
    }
  }
}

print(cards);

If it is a single result you can also do this:

var result = await db.first({'some':'filter'});

var cards = [];

if(result.containsKey('cards') && result['cards'] is Map){
  for(var key in result['cards'].keys){
    cards.add(result['cards'][key]);
  }
}

print(cards);

@stemuk
Copy link
Author

stemuk commented Jul 5, 2018

Your code works exacly as I hoped! Thanks a lot! 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants