I’m playing around with MongoDB with mongoose and come to a slight roadblock atm trying to implement searching within objects in a collection.
So I have a schema that is as follows:
var schema = mongoose.Schema({
form_id: Number,
author: Number,
data: String,
files: String,
date: { type: Date, default: Date.now },
});
The data is just a JSON object of key/values.
An example entry of a record:
{
"form_id" : 5,
"author" : 1,
"data" : "
{\"staff\":\"Joe Blow\", \"date\":\"25th Jan 2013\"}",
"_id" : ObjectId("5101fd4ee6ca550000000003"),
"date" : ISODate("2013-01-25T03:34:38.377Z"),
"__v" : 0
}
How do I search for a specific value inside the data object? I’m trying to do something like the following but not having any luck 🙁
db.forms.find({form_id: 5, data: '/Joe/i'});
If you omit the single quotes around the regular expression it should work:
But are you sure you want
datato contain a JSON string instead of an object? An object would give you so much more flexibility.