Categories
JavaScript Answers

How to search an array of objects in MongoDB?

Sometimes, we want to search an array of objects in MongoDB.

In this article, we’ll look at how to search an array of objects in MongoDB.

How to search an array of objects in MongoDB?

To search an array of objects in MongoDB, we can use the $elemMatch operator.

For instance, we write

db.users.find({
  awards: {
    $elemMatch: {
      award: 'Medal',
      year: 1975
    }
  }
})

to search the awards array in the users collection for users with an awards entry that has the award property set to 'Medal' and year set to 1975.

Conclusion

To search an array of objects in MongoDB, we can use the $elemMatch operator.

Categories
JavaScript Answers

How to push items into Mongo array via Mongoose?

Sometimes, we want to push items into Mongo array via Mongoose.

In this article, we’ll look at how to push items into Mongo array via Mongoose.

How to push items into Mongo array via Mongoose?

To push items into Mongo array via Mongoose, we can use the array push method.

For instance, we write

person.friends.push(friend);
person.save(done);

to call push on person.friends to append a new entry to the person‘s friends collection.

And then we call person.save to save the person entry.

Conclusion

To push items into Mongo array via Mongoose, we can use the array push method.

Categories
JavaScript Answers

How to get file extension with Node.js?

Sometimes, we want to get file extension with Node.js.

In this article, we’ll look at how to get file extension with Node.js.

How to get file extension with Node.js?

To get file extension with Node.js, we can use the path.extname method.

For instance, we write

const path = require('path')

const ext = path.extname('index.html')

to call path.extname with 'index.html' to return the extension of index.html.

Conclusion

To get file extension with Node.js, we can use the path.extname method.

Categories
JavaScript Answers

How to add user authentication for Node.js?

Sometimes, we want to add user authentication for Node.js.

In this article, we’ll look at how to add user authentication for Node.js.

How to add user authentication for Node.js?

To add user authentication for Node.js, we can use Passport.

For instance, we write

passport.use(new LocalStrategy(
  (username, password, done) => {
    User.findOne({
      username,
      password
    }, (err, user) => {
      done(err, user);
    });
  }
));

app.post('/login',
  passport.authenticate('local', {
    failureRedirect: '/login'
  }),
  (req, res) => {
    res.redirect('/');
  });

to add Passport to the Express app we have.

We call passport.use with a LocalStrategy instance to let us authenticate users by looking them up from our own MongoDB database.

We use User.findOne to find the user.

Then we add the /login route that uses the middleware returned by passport.authenticate for authentication before we redirect to /.

Conclusion

To add user authentication for Node.js, we can use Passport.

Categories
JavaScript Answers

How to stop Mongoose from creating _id property for sub-document array items?

Sometimes, we want to stop Mongoose from creating _id property for sub-document array items.

In this article, we’ll look at how to stop Mongoose from creating _id property for sub-document array items.

How to stop Mongoose from creating _id property for sub-document array items?

To stop Mongoose from creating _id property for sub-document array items, we can call mongoose.Schema with _id set to false.

For instance, we write

const mongoose = require("mongoose");

const subSchema = mongoose.Schema({}, {
  _id: false
});

const schema = mongoose.Schema({
  subSchemaCollection: [subSchema]
});

const model = mongoose.model('tablename', schema);

to create subSchema by calling mongoose.Schema with an object that has _id set to false.

Then we put the subSchema in the parent schema in the array we set as the value of subSchemaCollection.

Conclusion

To stop Mongoose from creating _id property for sub-document array items, we can call mongoose.Schema with _id set to false.