Fuzzy Searching with Mongodb?

I believe that to do “fuzzy” search you will need to use regex. This should accomplish what you’re looking for (escapeRegex function source here): function escapeRegex(text) { return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, “\\$&”); }; router.get(“https://stackoverflow.com/”, function(req, res) { if (req.query.search) { const regex = new RegExp(escapeRegex(req.query.search), ‘gi’); Jobs.find({ “name”: regex }, function(err, foundjobs) { if(err) { console.log(err); } … Read more

ISODate is not defined

Note that ISODate is a part of MongoDB and is not available in your case. You should be using Date instead and the MongoDB drivers(e.g. the Mongoose ORM that you are currently using) will take care of the type conversion between Date and ISODate behind the scene.

Mongoose password hashing

The mongodb blog has an excellent post detailing how to implement user authentication. http://blog.mongodb.org/post/32866457221/password-authentication-with-mongoose-part-1 The following is copied directly from the link above: User Model var mongoose = require(‘mongoose’), Schema = mongoose.Schema, bcrypt = require(‘bcrypt’), SALT_WORK_FACTOR = 10; var UserSchema = new Schema({ username: { type: String, required: true, index: { unique: true } }, … Read more

MissingSchemaError: Schema hasn’t been registered for model “User”

I got the same problem when I am trying the MEAN tutorial. After done a little bit research, I found that in app.js, if I put require(“./models/User”) before var routes = require(“./routes/index”), then it works. Like this: mongoose.connect(“mongodb://localhost/news”); require(“./models/Posts”); require(“./models/Comments”); var routes = require(‘./routes/index’); var users = require(‘./routes/users’); var app = express(); Hope the answer … Read more

getting schema attributes from Mongoose Model

Yes, it is possible. Each schema has a paths property, that looks somewhat like this (this is an example of my code): paths: { number: [Object], ‘name.first’: [Object], ‘name.last’: [Object], ssn: [Object], birthday: [Object], ‘job.company’: [Object], ‘job.position’: [Object], ‘address.city’: [Object], ‘address.state’: [Object], ‘address.country’: [Object], ‘address.street’: [Object], ‘address.number’: [Object], ‘address.zip’: [Object], email: [Object], phones: [Object], tags: … Read more

How do I get the objectID after I save an object in Mongoose?

This just worked for me: var mongoose = require(‘mongoose’), Schema = mongoose.Schema; mongoose.connect(‘mongodb://localhost/lol’, function(err) { if (err) { console.log(err) } }); var ChatSchema = new Schema({ name: String }); mongoose.model(‘Chat’, ChatSchema); var Chat = mongoose.model(‘Chat’); var n = new Chat(); n.name = “chat room”; n.save(function(err,room) { console.log(room.id); }); $ node test.js 4e3444818cde747f02000001 $ I’m on … Read more