using this line :
var collection = mongoose.connection.db.collection("users");
I get users s collection, now i need to get the schema from that collection ?
Is there a way to do this? thank you.
PS: I dont have any models
if you do not have any models, I'm afraid this can't be done.
the schema is the structural representation of the document, not the collection. You can use the schema (via the model) to enforce that any document added to the collection match some structure, but there's nothing that prevents you from inserting into this collection a different document without this model, that will not match the criteria
so the schema does not live in the DB, it is a part of the mongoose module, and lives in the code. and the way to access it is through the model only
Related
I was learning Node.js and Mongoose. Then, I cam across the term schema which is defined as the description of the structure of the data, default values and validation. So, as we know schema looks basically like this:
var TaskSchema = new Schema({
name: String,
priority: Number
});
The question I would like to ask is Why should we care about the description of structure of data that the schema allows to achieve, I mean, what is the main point in the use of schemas in mongoose? if you say, validation, then can't we achieve that with express-validator package? or...
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
See the documentation for more information.
Schemas not only define the structure of your document and casting of properties, they also define document instance methods, static Model methods, compound indexes, and document lifecycle hooks called middleware.
This is what allows you to communicate with the database easily using mongoose. It's not just for validation.
I'm using mongoose 5.0.9. My question is about the _id of document.
I'd like to get `_id generated from mongodb when new document inserted. In this way, I could not define the _id in my mongoose schema/model. If I do, the mongoose will fail since I don't provide the value for _id.
However, I want to use the _id in my page. If I don't define it in my schema/model, I could not get it when querying.
One solution I thought is to define two schemas: one for inserting document without _id, and the other is for querying, updating, etc, with _id.
I don't want to make things to complex, so I don't think this is good idea.
what is the best practice for this _id?
Thanks in advance.
Richard
I have Mongoose schema and I need to add a non-persistent field to it. The point of this field is to store some status, related to persistent data fields, but without need to store it to the database. I see that some Mongoose alternatives provide such a feature e.g. https://github.com/simpleviewinc/mongolayer#modeladdfieldargs, however I am not able to find similar one inside Mongoose.
Any tip would be greatly appreciated!
In mongoose you have the concept of virtual fields. See doc (http://mongoosejs.com/docs/guide.html#virtuals).
See also related topic at Adding 'virtual' variables to a mongoose schema?
In mongodb I have one collection named as People.
It contains:
_id:Number,
name:String,
friends:[{friendsId:{type:Schema.Types.ObjectId,ref:'People'},addedTime: Date}]
I want to search a name of friend of a particular user and return the _id of that friend in a single query using mongoose in node.js.
Its impossible to retrieve the data in a single query/populate. Since mongodb is not a relational database. Iterate the data separately & give the result as callback to next query.
Using Mongoose, I have a document that was previously pulled from the database, complete with an _id property, in raw Object format (IE, without all of the document methods, just straight from the db).
How can I use that data to create an instance of mongoose.Model without the system assigning the model a new _id? I want to then eventually save that model and have it update the existing document in the database.
Update: using a combination of #Jack Newcombe's method, and subsequently setting model.isNew to false, I get the following error: "Mod on _id not allowed". So now it knows to update, but Mongoose is not removing the _id field from the update request. There has to be one more system property on the Model that tells Mongoose whether or not to remove the _id during an update request. Any ideas?
I've figured it out, but I'm sure there is a better way. I'm surprised there isn't a way to do this easily with Mongoose's API.
Anyway, you need to the following:
Create the model like this: var model = new Model(data,
{_id:false});
Manually set model.isNew to false
Manually tell Mongoose that the _id field hasn't been modified, like this: delete model.$__.activePaths.states.modify._id
The reason I'm not so fond of this is because I'm taking advantage of the fact that JavaScript doesn't have true protected methods, and I'm basically hacking Mongoose in order to get it to work. So I'd love to hear other answers if anyone has.
You can prevent the schema for the model from automatically generating an _id by passing in an option that sets the _id to false:
var schema = new Schema({ name: String }, { _id: false });
Source: http://mongoosejs.com/docs/guide.html#_id