Reputation: 41587
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'});
Upvotes: 0
Views: 182
Reputation: 594
maybe you want this?
db.forms.find({form_id: 5, "data.staff": /Joe/i});
if your 'data' property of forms is an object. and it's way better.
Upvotes: 0
Reputation: 311835
If you omit the single quotes around the regular expression it should work:
db.forms.find({form_id: 5, data: /Joe/i});
But are you sure you want data
to contain a JSON string instead of an object? An object would give you so much more flexibility.
Upvotes: 2