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.
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.
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.
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.
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.
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!
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:
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:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With