2

I am trying to figure out how to query the nested objects inside the Components object. The data was inserted from a parsed json file.

Query

var query = {}
cursor = db.collection("workflows").find(query).toArray(function(err, result) {
if (err) throw err;

console.log(result);
db.close();
    });

This data is returned when I run the query above:

At this point i'm just trying to get it to filter in some manner. I've tried Name:'Test WF' and other variations of that but still can't get a filtered response.

[ { _id: 5c77040838f9d322b89bbd82,
texto:
 { _id: 12,
   LocalCachePath: 'Z:\\Test\\Cache',
   SharedCachePath: [],
   Name: 'Test WF',
   Desc: 'I\'m testing',
   Components: [Array] } },
{ _id: 5c7704164413692978a9dd1a,
texto:
 { _id: 'Workflow-2019.02.22-23.21.15-MKRU',
   LocalCachePath: 'Z:\\MAITest\\Cache',
   SharedCachePath: [],
   Name: 'Test WF',
   Desc: 'I\'m testing',
   Components: [Array] } },
 { _id: 5c77046335b012379c99951b,
texto:
 { _id: '154',
   LocalCachePath: 'Z:\\Test\\Cache',
   SharedCachePath: [],
   Name: 'Test WF',
   Desc: 'I\'m testing',
   Components: [Array] } },
 { _id: 5c7704787bde6f36543d1016,
texto:
 { _id: 'Workflow-2019.02.22-23.21.15-MKRU',
   LocalCachePath: 'Z:\\Test\\Cache',
   SharedCachePath: [],
   Name: 'Test WF',
   Desc: 'I\'m testing',
   Components: [Array] } } ]

Any insight would be helpful i'm stumbling through this one step at a time.

Here's another query that is giving me results but i guess my issue is going to be to parse out my results as variables.

var query = {'texto.Components.0.Name' : {$gt: ''}}
// var query = {'testo.Name' : {$gt: ''} }
 cursor = db.collection("workflows").find(query).toArray(function(err, result) {
    if (err) throw err;
3
  • Nice Desc! What do you mean by "filter in some manner"? Can you be more specific? Commented Feb 27, 2019 at 22:35
  • What exactly do you want to have returned? Commented Feb 28, 2019 at 0:14
  • I really want to get to the Components level and access the data in their sub nodes. I would like to be able to pull the field name and value for each node in the file. Commented Feb 28, 2019 at 13:35

3 Answers 3

4

Use dot notation (e.g. texto.Name) to query and retrieve fields from nested objects, example:

var query = {'texto.Name': 'Test WF'}
Sign up to request clarification or add additional context in comments.

Comments

1

Simply

db.getCollection('TestQueries').find({'texto.Name': 'Test WF'})

Regex used for Case Insensitive.

db.getCollection('TestQueries').find({"texto.Name":{
                                 '$regex' : '^test wa$', '$options' : 'i'
                                 }})

Using collation

db.fruit.createIndex( {"texto.Name": 1},{ collation: {
                     locale: 'en', strength: 2 
                     } } )

db.getCollection('TestQueries').find( 
            { "texto.Name": "test wa" } ).collation( { locale: 'en', strength: 2 } 
            )

3 Comments

Thank you for your input, this is what i have but i'm getting db.getCollection is not a function var query = {'texto.Name': 'Test WF'} cursor = db.getCollection('workflows').find({query})(function(err, result) { if (err) throw err; console.log(result); db.close(); });
I just run this on Robo 3T. Thats why i am used getCollection. I think u can try with db.collection db.collection()
i have a same data flow but it is giving all data instead of matched data below is my code. { "$match": { "$or": [ { "product_categories.product_list.name": { "$regex": ".*samo.*", "$options": "i" } } ] } }
0

You can also use $elemMatch. It is longer, but allows for multiple fields query.

db.getCollection('TestQueries').find({texto: {$elemMatch: {Name: "test wa"} }))

Official docs here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.