How to query a field inside array sub-document using ottomanjs using query builder

237 Views Asked by At

I could't able to query using arrays sub-document field using ottomanJS and I cant find any documentation for query building for array sub-document, any help would be appreciated.

import { Schema, Query, getDefaultInstance } from "ottoman";

const  TestSchema = new Schema({
block1:{
    field1:{type: String, required: true},
    field2:{type: String, required: true}
},
block2:[{
    field3:{type: String, required: true},
    field4:{type: String, required: true}
}]
})

const ottoman = getDefaultInstance();
const query = new Query({}, 'bucketName.scopeName.collectionName');
const where_exp = {block2[{field3}]:{$eq: 'xyz'}};
const result = query
  .select()
  .where(where_exp)
  .build();
console.log(result);
1

There are 1 best solutions below

0
On
const where_exp = {
$any: {
          $expr: [{ b: { $in: ‘block2’ } }],
          $satisfies: { ‘b.field3’: {$eq: “xyz”} },
     }

};

//this will produce this valid N1Ql query SELECT * FROM travel-sample.inventory.hotel WHERE ANY b IN block2 SATISFIES b.field3=“xyz”

Here is the answer I got from couchbase forum incase anyone still looking for