Get list of attributes from mongodb object

It is clear that since the database is smaller than the schema, we cannot. But if we take one record, for example, db.collectionname.findOne (), this is not a smaller schema. It has fixed attributes. How to get this attribute less?

+5
source share
1 answer

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 is just an β€œobject”)

This shows the β€œoutline” of only the top level, if you want to look deeper, you need some recursive tree walk function.

+8
source

All Articles