0

I've a document with a nested json data. It has 15 text and 10 integer datatypes.

How can I write a query such that any text or integer that matches should be displayed?

I tried the following query but it did not work as expected.

db.customers.find({ sno : /ab/ },{ name : /ab/ }).count()

I'm getting 0 count as result. I'm looking at OR clause and not AND clause.

jason
  • 3,932
  • 11
  • 52
  • 123

1 Answers1

1

You could specify every field within a $or statement:

 db.customers.find({$or: [
     {field1: /ab/ },
     {field2: /ab/ },
     {field3: /ab/ },
     {field4: /ab/ },
     {field5: /ab/ }
 ]}).count()

You could also use a text query, for this you'll have to create a text index:

db.collection.createIndex( { "$**": "text" } )

Then query it using:

db.articles.find( { $text: { $search: "ab" } } )

Text query - https://docs.mongodb.com/manual/reference/operator/query/text/ Text index - https://docs.mongodb.com/manual/core/index-text/

Kevin Smith
  • 13,746
  • 4
  • 52
  • 77
  • Thank You, Kevin for your response! How can I convert this query in pymongo ? I tried adding quotes and double quotes everywhere but did not get right response . – jason Dec 27 '17 at 04:33
  • You need to create a regex, see this answer :https://stackoverflow.com/a/4877662/4079967 – Kevin Smith Dec 27 '17 at 09:27