1

My question was, how to find all title of the releases contains the artist name Here is the 2 Documents for just an example:

`{"release" : {"title" : "DEF day",
            "artists" : {"artist" : {"role" : "1","name" : "DEF"}}}
    }

{   "release" : {"title" : "XYZ day",
             "artists" : {"artist" : {"role" : "1","name" : "KYC"}}}
    }`

when i run this following query:

`db.test.find({$where:
  "this.release.title.indexOf(this.release.artists.artist.name) > -1"

})`

I get the result "DEF day", so it works excellent!! BUT, once i run this query to my original data(same format like above) i get this:

`error: {
"$err" : "TypeError: Object 30 has no method 'indexOf'\n    at _funcs1 (_funcs1:1:49) near 'his.release.artists.a' ",
"code" : 16722

}`

My collection is big (8GB+). Just looking at the above error, can anyone tell what may be the problem with the above query? your answer is appreciated. please.

4
  • can you run the following query and post its output? find({ field: { $type: 16 } })
    – bagrat
    Commented Jun 17, 2015 at 18:40
  • @n9code Originally this collection is about 20GB. i have a small sample of 300MB. Also the 'releases' has many more fields other than title, artists. btw, with running your code with the 300MB sample here is the rusult: here is the result: Fetched 0 record(s) in 9646ms
    – mrana
    Commented Jun 17, 2015 at 19:28
  • the following query giving me the all exact matching (releases and artist) names: db.releases.find({$where: "this.release.title == this.release.artists.artist.name" }) but i need titles which contains the artist name.
    – mrana
    Commented Jun 17, 2015 at 20:29
  • no answer from anyone?
    – mrana
    Commented Jun 18, 2015 at 7:16

1 Answer 1

0

The problem is in the data itself. Somewhere you have a document, where the title field is not String, thus invoking indexOf on the whole data, on that particular document you get the error.

One thing you can do to solve this problem is first of all find that erroneous document. To do that you will need to run a simple query.

MongoDB has the $type operator, which matches fields by their type. And here is the list of all available BSON types a MongoDB document field can have.

The id of String BSON type is 2, thus you need all the documents that have type of title field not equal to 2.

find({field: {$not: {$type: <BSON type>}}}))

After finding the document(s), you may fix or remove them.

Not the answer you're looking for? Browse other questions tagged or ask your own question.