MongoDB Flashcards

1
Q

In MongoDB, how do you create a collection and add an item to that collection at the same time?

A

db.db_name.insertOne( {“test”: “test”} )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In MongoDB, how do you switch between DBs?

A

use db_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In MongoDB, how do you delete a collection?

A

db.db_name.drop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between the native MongoDB driver for node and Mongoose?

A

mongoose holds the schemas within the application code, whereas the mongodb driver relies on the schemas set within the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the syntax for creating a Mongoose item Schema?

A
const ItemSchema = new Schema({
    field_name: field_type,
    required: true
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In Mongoose, how do you create a model?

A

Define the structure of the model and then export it to be received by other local files using:

module.exports = Item = mongoose.model(‘model_name’, ModelSchema)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In Mongoose, what is the syntax for posting a new item to MongoDB?

A
router.post('/path/to/api', (req, res) => {
    const newItem = new Item({
        name: req.body.name
    });
newItem.save()
    .then(item => res.json(item)
    .catch(err => res.json({ message: err }); });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In Mongoose, what is the syntax for deleting an item from MongoDB?

A

router.delete(‘/path/to/api/:id’, (req, res) => {
const itemId = req.params.id;
Item.findById(itemId)
.then(item =>
item
.remove()
.then(() => res.json({ message: itemId }))
.catch(err =>
res.status(404).json({ message: err }));
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly