Vojtěch Janoušek
Vojtěch Janoušek

Reputation: 43

How to get schema from existing collection MongoDB

I have database "Data", collection "licence" and collection is full of data. How can I get schema from collection and print to console? Without using Mongoose. Thank you

MongoClient.connect(url, function(err, db) {
if(err) throw err;
var dbo = db.db("data");
var schematodo = dbo.collection("licence").findOne(obj, function(err, res){
    for (var key in schematodo) {
        
        console.log(indent, key, typeof schematodo[key]) ;
    };
});

db.close();

});

Upvotes: 2

Views: 2129

Answers (1)

Vojtěch Janoušek
Vojtěch Janoušek

Reputation: 43

I found out solution for my problem.

MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
    if (err) return console.error(err);
    const db = client.db("data");
    parseSchema(db.collection('licence').find().limit(1), {semanticTypes: true},function(err, schema) {
        if (err) return console.error(err);
        console.log(schema);
        schema.fields.forEach(element=>{
            console.log(element.name + " - " + element.type);
        });
        client.close();
    });
});

Upvotes: 1

Related Questions