need result as per the document below
// Populate using Model
const stories = await Story.find({ title: ‘Casino Royale’ }, {populate: ‘author’})
const story = stories[0];
console.log(‘The author is %s’, story.author.name);
// prints “The author is Ian Fleming”
my example is :
const Services= new Schema({
service: String,
serviceId: String,
serviceCode: String,
});
module.exports =connection.model(“Services”, Services);
var schema = new Schema({
agentId: { type: String },
name: { type: String },
mobile: { type: String },
Services: [{ type: Services, ref: “Services” }],
atype: { type: String },
});
schema.index.findByAgentID = {
by: ‘agentId’,
type: ‘refdoc’,
};
schema.index.findByAType = {
by: ‘atype’,
type: ‘refdoc’,
};
module.exports =connection.model(“Agent”, schema );
but not populate services
Que. 1: Not populate using find as per ottoman document
const result = await Agent.find({ atype: “agent” }, { populate: ‘Services’ });
got error Unable to populate field “Services”, it is not available on select clause
Que. 2: if I use findByAType then working fine but at the insert time got error document already exists.
because I add atype=“agent” for all data,
please tell me how to get all the agent data lists of an agent with populated data?
expected result :
[{
“agentId”:“124554”,
“name”: “Suresh”,
“mobile”: “8898989817”,
“atype”:“agent”,
“Services”: [
{
“serviceId”: “16546555”,
“service”: “HOTEL”,
“serviceCode”: “HT”,
},
{
“serviceId”: “16546556”,
“service”: “Airline”,
“serviceCode”: “AL”
}
]
},{
“agentId”:“123456”,
“name”: “Amit”,
“mobile”: “8898989818”,
“atype”:“agent”,
“Services”: [
{
“serviceId”: “16546555”,
“service”: “HOTEL”,
“serviceCode”: “HT”,
},
{
“serviceId”: “16546556”,
“service”: “Airline”,
“serviceCode”: “AL”
}
],
}]