Szukaj…


Podstawowy schemat

Podstawowy schemat użytkownika:

var mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
    name: String,
    password: String,
    age: Number,
    created: {type: Date, default: Date.now}
});

var User = mongoose.model('User', userSchema);

Rodzaje schematów .

Metody schematu

Metody mogą być ustawione na schematach, aby pomóc w robieniu rzeczy związanych z tymi schematami i utrzymywać je w dobrej organizacji.

userSchema.methods.normalize = function() {
    this.name = this.name.toLowerCase();
};

Przykładowe użycie:

erik = new User({
    'name': 'Erik',
    'password': 'pass'
});
erik.normalize();
erik.save();

Schemat statyczny

Schematy statyczne to metody, które można wywoływać bezpośrednio przez model (w przeciwieństwie do metod schematu, które muszą być wywołane przez instancję dokumentu Mongoose). Do schematu przypisuje się wartość statyczną, dodając funkcję do obiektu statics schematu.

Jednym z przykładów użycia jest tworzenie niestandardowych zapytań:

userSchema.statics.findByName = function(name, callback) {
    return this.model.find({ name: name }, callback);
}

var User = mongoose.model('User', userSchema)

User.findByName('Kobe', function(err, documents) {
    console.log(documents)
})

Schemat GeoObjects

Ogólny schemat przydatny do pracy z obiektami geograficznymi, takimi jak punkty, linie i wielokąty. Zarówno Mongoose, jak i MongoDB obsługują Geojson .

Przykład użycia w Node / Express :

var mongoose = require('mongoose');
var Schema   = mongoose.Schema;

// Creates a GeoObject Schema.
var myGeo= new Schema({
                        name: { type: String },
                        geo : {
                                type : {
                                         type: String, 
                                         enum: ['Point', 'LineString', 'Polygon']
                                },
                                coordinates : Array
                         }
});

//2dsphere index on geo field to work with geoSpatial queries
myGeo.index({geo : '2dsphere'});
module.exports = mongoose.model('myGeo', myGeo);

Oszczędzanie bieżącego czasu i czasu aktualizacji

Ten rodzaj schematu będzie przydatny, jeśli chcesz śledzić swoje elementy według czasu wstawienia lub czasu aktualizacji.

var mongoose = require('mongoose');
var Schema   = mongoose.Schema;

// Creates a User Schema.
var user = new Schema({
                       name: { type: String },
                       age : { type: Integer},
                       sex : { type: String },
                       created_at: {type: Date, default: Date.now},
                       updated_at: {type: Date, default: Date.now}
});

// Sets the created_at parameter equal to the current time
user.pre('save', function(next){
    now = new Date();
    this.updated_at = now;
    if(!this.created_at) {
        this.created_at = now
    }
    next();
});

module.exports = mongoose.model('user', user);


Modified text is an extract of the original Stack Overflow Documentation
Licencjonowany na podstawie CC BY-SA 3.0
Nie związany z Stack Overflow