Mongo has a sophisticated query syntax that is full of options. Too many to list here. Using a mongoose model, we have a few ways to ask mongo about what data we want.
Model.find({}, function(err, documents) {
if (err) {
next(err);
} else {
res.json(documents);
}
});
Above is a basic example of how we can query mongo for a specific model. Using the .find() method, we can pass in a query object to specify what we're looking for. The object is empty so we want all instances of the collection. The second argument is a node style callback with either an error or the documents. Most queries on mongoose return a promise as well. Look here at the mongoose docs
Model.findById('57490284328430', function(err, doc) {
if (err) {
next(err);
} else {
res.json(doc);
}
});
Above is a strategy we would use to to find one document from a collection given the id of the document. Helpful for those PUT/DELETE/GET one routes.
// few ways to create a new document
var dog = new Dog({
name: 'Bingo'
});
dog.save(function(err, savedDog) {
if (err) {
next(err)
} else {
res.json(savedDog);
}
});
/////////////////////
Dog.create({name: 'Bingo'}, function(err, savedDog) {
})
//////////////////////
var dog = new Dog();
dog.name = 'Bingo';
dog.save(function(err, savedDog) {
});
Above is 3 ways to create a new document for a given collection. Basically we have to create a new instance of the model, add the properties to it, then call .save(). The .create() method does this internally.
Model.findByIdAndUpdate('28393928392', {name: 'new name'}, function(err, updatedDoc){
if (err) {
next(err)
} else {
res.json(updatedDoc);
}
});
Above is an example of how we can update a document given its id. Notice that most of the operations we want to do with mongoose look very similar. There are advanced uses and features as well. Along with performing methods on the model, we can also use the actuall document to do things. Operations on the document are atomic.
Model.findOne({name: 'Jan'}, function(err, doc) {
doc.remove(function(err, removedDoc) {
// just deleted the document from the DB
});
})
This is possible because the actual documents returned from mongo are not just objects but instances of a collection and have specific properties and methods that allow us to operate on it and have it write back to the DB.
Monog is no NoSql DB so we don't have join tables, but we need a way of seeing relational data. The solution for that is call population. You can thinkof it as a join table at call time.
var DogSchema = new mongoose.Schema({
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: 'person'
},
name: String
});
var PersonSchema = new mongoose.Schema({
name: String
});
var Dog = mongoose.model('dog', DogSchema);
var Person = mongoose.model('person', PersonSchema);
// find all dogs and populate their owners
// this will grab the ids on the owners field
// and go to the ref, which is the person model
// and grab the person doc with the matching id
// and place the object on the owners field
var promise = Dog.find({})
.populate('owner')
.exec();
promise.then(function(dogs) {
}, function(err) {
});
There is so much to do and so many ways to do it with mongoose and mongo. Its another course on its own. This should be enough to get you started with our basic needs for our api