Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get attribute list from mongodb object

It is understandable that since the database is schema less we are not able to But if we take one record e.g db.collectionname.findOne(), it is not schema less. It has fixed attributes. How do I get that attribute less ?

like image 833
Succeed Stha Avatar asked Jul 25 '11 10:07

Succeed Stha


1 Answers

the code:

> db.mycoll.insert( {num:3, text:"smth", date: new Date(), childs:[1,2,3]})
> var rec = db.mycoll.findOne();

> for (key in rec) { 
    var val = rec[key];
    print( key + "(" + typeof(val) + "): " + val ) }

will print:

_id(object): 4e2d688cb2f2b62248c1c6bb
num(number): 3
text(string): smth
date(object): Mon Jul 25 2011 15:58:52 GMT+0300
childs(object): 1,2,3

(javascript array and date are just "object")

This shows "schema" of only top level, if you want to look deeper, some recursive tree-walking function is needed.

like image 185
zmila Avatar answered Oct 11 '22 19:10

zmila