Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose: Generate Schema from existing data

For example, I have user collection in db:

{'_id':0, 'name': 'Joe', 'score':[80, 33]}
{'_id':1, 'name': 'Moe', 'score':[90, 81]}
... ...

How can I read this data with existing format, which means, use it's existing schema without create a new one.

I read Mongoose doc and googled for a while but didn't find satisfied answer.

like image 873
wilbeibi Avatar asked Jun 19 '14 19:06

wilbeibi


People also ask

How do I create a schema in MongoDB?

Basically, MongoDB is schema-less database, we cannot create schema in MongoDB, but we enforce the collection documents in application code or using MongoDB atlas GUI tool. For generating schema first we need to connect to the specified database and collections in MongoDB.

What does $Set do in Mongoose?

Mongoose getters and setters allow you to execute custom logic when getting or setting a property on a Mongoose document. Getters let you transform data in MongoDB into a more user friendly form, and setters let you transform user data before it gets to MongoDB.

What is the difference between a Mongoose schema and model?

Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

How do I create a schema in mongoose?

Create a folder and initialize the project using npm init. Create an app.js file and install mongoose using the below command. Use the Mongoose.Schema () function to specify the model and then use it to save data. Create an object of that model filling the information and use the mongoose function to save the data to the database.

Is it possible to make a model from a mongoose database?

It will work if you make a model with the same schema. Mongoose is an ODM, so you need a schema to create objects. If you need to run queries and get raw data from the database, I would stick with this library: Thanks for contributing an answer to Stack Overflow!

Is it possible to make a model with the same schema?

It will work if you make a model with the same schema. Mongoose is an ODM, so you need a schema to create objects. If you need to run queries and get raw data from the database, I would stick with this library:

How do I add a collection to a MongoDB schema?

The way to do this is to add the name of the collection to your schema. So if your collection is in 'mongodb://localhost:27017/test' and called 'things', you would use:


2 Answers

When using mongoose with an existing collection, you have to reference the collection in your mongoose schema. The way to do this is to add the name of the collection to your schema. So if your collection is in 'mongodb://localhost:27017/test' and called 'things', you would use:

~~~
const Schema = mongoose.Schema;
const ThingSchema = new Schema({
  name: {
    type: String
  }
});

const Model = mongoose.model('Thing', ThingSchema, 'things');
module.exports = Model;
~~~

Thanks to http://chrisflx.blogspot.fr/2014/04/nodejs-mongoose-with-preexisting-data.html?m=1

like image 133
armand Avatar answered Oct 03 '22 09:10

armand


It will work if you make a model with the same schema.

var schema = new mongoose.Schema({ name: 'string', score: [] });
var user = mongoose.model('User', schema);

Edit:

Mongoose is an ODM, so you need a schema to create objects. If you need to run queries and get raw data from the database, I would stick with this library:

https://github.com/mongodb/node-mongodb-native

like image 43
tpae Avatar answered Oct 03 '22 11:10

tpae