Sign In Register

How can we help you today?

Start a new topic
Answered

Get dictionary from Collection.

Hello.

I'm trying to get a document that contains a dictionary from a collection using an id of an item in that dictionary. 

For example, I have a dictionary in each document, and I want to find all Dictionaries that contain an item with id "3", How Can I do that?

(Please Check the photo in the attached files for example of the dictionary)


I tried to use that in cloud code:

collection.find("DictionaryName" , "3");

Note: 3 here is the item id.

But that didn't work, I'm getting Null.

Can you help me with that? 

Thanks.


Best Answer

Hi Yaser,


Am I correct in saying you are attempting to retrieve a document from a collection based on the value of an embedded document? You can do this using dot notation. For example, if I had:


{

    "id":1,

    "embeddedDoc":{

        "id":2 

    }

}


You could find this document using the query {"embeddedDoc.id":2}


If you have any further questions please let us know.


Regards,

Vinnie


Hi Yaser Prog


Can you give me a small example of these dictionaries and what response you want from the query?

Would all the items with the value "3"  your looking for have the same key in the dictionary.


Regards

Katie

Hi, thanks for your response.

Sorry I forgot to add the picture in the attachments, I will add it here .

This is an example of a dictionary in a document 

image


As for your question, the items might have the same key which is, for example, "3".

Like in the picture above, the id under ThrustInfo is a key for an item in the dictionary, this id represents a player id, so any dictionary can have the same player id as an item key,  and I want to get the item from the dictionary by it's key, i.g. player id.


Hello, Katie.

Can you help me in this issue? 

Thanks

Hi Yaser,


The structure of your document isn't going to lend itself well to the kind of query you are trying to implement. The only true suggestion I can make it that you rethink how you are going to store this player information, mainly it is difficult to both search an array and to be looking for a particular key instead of a value. In this case the value would be a JSON document which would be impossible to search on. One suggestion would be to store this information inside a players private data. This way anytime you are looking for a players record you can get it from the context of that player within cloud code like so.


var info = Spark.getPlayer().getPrivateData("Data");


Another suggestion would be to break this array into individual documents per player, that way you can search the playerId as a value rather than as a key.


Does this seem like viable solution to you?


Kind regards,

 - Steve

Hi, Thanks for your response.

Well, I wanted to create a document for each dictionary to avoid saving lots of data on the server because each dictionary will contain an item for each player so each player can search for dictionaries that contain an item with his id as a key and get those items.

And I guess that breaking the dictionary into documents for each player would make the collection too large and harder to find items from it (correct me if I'm wrong please).

As for player private data, can I save a dictionary using JSON in it? because each object in private data needs a key, and i have lots of objects to save.  


Hi Yaser,


That is one of the best features of MongoDb, it is very good at finding a few needles in many haystacks. I'm not sure I fully understand what you mean when you say "save a dictionary using JSON in it". Yes you can also store arrays within a players private data.


I hope this helps,

 - Steve

Hello, thanks, Steve.

So If I saved some documents, with each one containing an object, that has a variable called id, and each object in each document may have the same value of id, can I find the documents by that variable? Like:

collection.find("ID",playerID);

this may result in one or more object, can it be returned as array?

Hi.

I tried it that way and saved a document that contains one object, then tried to get that object by a variable in it from the No Sql Explorer, but I couldn't get the object, I didn't get any result.

Can you help me in that? 

Thanks, Steve.

Answer

Hi Yaser,


Am I correct in saying you are attempting to retrieve a document from a collection based on the value of an embedded document? You can do this using dot notation. For example, if I had:


{

    "id":1,

    "embeddedDoc":{

        "id":2 

    }

}


You could find this document using the query {"embeddedDoc.id":2}


If you have any further questions please let us know.


Regards,

Vinnie

Thanks for your response, Vinnie.

Yes that's exactly what I wanted.

Another question please: If I did that in cloud code, then that will return the embedded document as an object of its original type? and if the query returned more than one document, it will return a cursor I believe, how can I return it in the scriptData of the request? 

Sorry for asking too many questions.

Hi Yaser,


No problem. It will actually return the entire document. You can add a projection to your query to only return the field or fields you require. For example:


var result = Spark.runtimeCollection("myCollection").find({"embeddedDoc.id":2},{"embeddedDoc":1,"_id":0})


will return only the value associated with the 'embeddedDoc' field (_id will always return unless you specify that you don't want it with _id:0).


To return the result in your request's scriptData, you'll need to iterate over the cursor and use it's contents to generate an array or object that you can then set to scriptData.


Regards,

Vinnie

Hello.
I tried that in cloud code, but the cursor returned doesn't contain any object.
here's my code from the cloud code:
var player_id = Spark.getPlayer().getDisplayName();
var player_thrusts = thrusts.find({"ThrustInfo.player_id" :player_id} );
Thanks for sticking with me so far, I appreciate it.

Hi Yaser,


An empty cursor would suggest that no documents in that collection match your query. Have you tried performing this query using the NoSQL explorer? Do you see any results here?


Regards,

Vinnie

Hello.

Yes, I tried that query in NoSQL explorer and it worked.

I have also tried to use another variable than the id in the cloud code, but it didn't work too.


Login to post a comment