retrieve Document within document within document

70 views
Skip to first unread message

Dorf

unread,
Jan 12, 2012, 10:09:02 PM1/12/12
to mongodb-user
How do i retrieve the value from a document within a document within a
document?

Here is the code i have atm
var locationAccuracy =
item["Address.Meta"].AsBsonDocument["Meta"].AsBsonDocument["LocationAccuracy",
null];

also how would i go about retrieving values from a document within an
array?

Robert Stam

unread,
Jan 13, 2012, 11:16:56 AM1/13/12
to mongod...@googlegroups.com
To retrieve a value from a document within an array:

BsonArray array;
var value = array[0].AsBsonDocument["x"].AsInt32;

Or replace AsInt32 with something else if the values are not integers.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Dorf

unread,
Jan 15, 2012, 5:07:55 PM1/15/12
to mongodb-user
Hey Robert I am not quite sure of the exact syntax could you
please take a look at this bit of my code and let me know if i am
leaning in the right direction


var cursor = col.FindAll();
foreach (var item in cursor)
{
BsonArray Phones;
var phone =
item["Phones[0]"].AsBsonDocument["Number"].AsString;
}

my mongodb for this bit is
"Phones" : [{
"Number" : "(08)85682115",
"Type" : 0.0,
"VisibleTo" : 0.0
}],

I get the error" Element 'Phones[0]' not found."

Robert Stam

unread,
Jan 16, 2012, 12:29:49 AM1/16/12
to mongod...@googlegroups.com
The [0] index needs to be outside of the field name. Something like:

var phone = item["Phones"].AsBsonArray[0].AsBsonDocument["Number"].AsString;
Reply all
Reply to author
Forward
0 new messages