How to Flatten json array in spark RDD - apache-spark

I have created spark RDD using MongoDB data. After creating RDD I got this result.
Sample result set:
Document{{_id=5737782,
SurveyNo=54ebc800-6gd3f-11e6-8ccb-ef65b1f62b86,
currenceNo=b5ae2a30-6e09-11e6-b9e1-757ghs218348,
"QA" : [
{
"QuestionId" : "8cdffd91-1bad-11e5-aa85-d53d21cd02a4",
"QuestionText" : "How are you?",
"ScaledAnswerValue" : NumberInt(75),
"AnswerValue" : NumberInt(3),
"AnswerText" : "Horrible",
"AnsweredDate" : ISODate("2014-12-10T07:00:43.958+0000"),
"Order" : NumberInt(0),
"Status" : "Failed",
"AnswerSelectors" : [
{
"Value" : NumberInt(0),
"Text" : "Horrible",
"Order" : NumberInt(1)
},
{
"Value" : NumberInt(1),
"Text" : "Poor",
"Order" : NumberInt(2)
},
{
"Value" : NumberInt(2),
"Text" : "Fair",
"Order" : NumberInt(3)
},
{
"Value" : NumberInt(3),
"Text" : "Good",
"Order" : NumberInt(4)
},
{
"Value" : NumberInt(4),
"Text" : "Ex`enter code here`cellent",
"Order" : NumberInt(5)
}
],
"AnswerType" : "Mood"
}
]
I want to select only QA.QuestionId,QA.QuestionText,QA.ScaledAnswerValue. how I can flat this QA array.

Related

How to find triple conditions in MongoDB

I have database like below.
{
"_id" : ObjectId("628bed8298b81711c01b6ab1"),
"status" : true,
"city" : "Kefar Sava",
"request" : [
{
"deadline" : {
"from" : ISODate("2022-05-03T00:00:00.000+0000"),
"to" : ISODate("2022-05-31T00:00:00.000+0000")
},
"ownerId" : "628c424431884d1c140f56bc",
"type" : "Developer",
"name" : "Apollo",
"status" : "posted",
"_id" : ObjectId("628d496d38360a30a4c39ebd"),
},
{
"deadline" : {
"from" : ISODate("2022-05-03T00:00:00.000+0000"),
"to" : ISODate("2022-06-02T00:00:00.000+0000")
},
"ownerId" : "628c424431884d1c140f56bb",
"type" : "Developer",
"name" : "Apollo",
"status" : "posted",
"_id" : ObjectId("628d4a00a4ec7e12a02e74a3"),
},
],
"date" : ISODate("2022-05-23T20:24:34.052+0000"),
"__v" : NumberInt(0),
}
I want to find by ownerId, type, city and set status to canceled.
I use
const owner = await User.findOneAndUpdate(
{$and: [{city: city, "request.ownerId": ownerId, "request.type": type }]},
{$set: {"request.$.status": "canceled"}},
{new: true})
But I can't find by type. When I change order of that(ownerId and type), then I can't find by ownerId.
Please tell me what's wrong with my code.

How to match inside $lookup used of MongoDB aggregation?

This is the query I've been using for getting some random uniqueXperiences, in that I've lookup xpert and I want to project its _id and name.
db.getCollection('uniquexperiences').aggregate([{
'$sample': {
'size': 2
}
}, {
'$lookup': {
'from': 'users',
'as': 'Xpert',
'pipeline':[{
'$project': {
'name': 1
}
}]
}
}]);
The output I'm getting is something like this:
/* 1 */
{
"_id" : ObjectId("5b558b5997199d3ec4953e6b"),
"updatedBy" : ObjectId("5b433dbf6a93e56a21f610eb"),
"updatedAt" : ISODate("2018-10-10T10:56:36.952Z"),
"createdBy" : ObjectId("5b433dbf6a93e56a21f610eb"),
"createdAt" : ISODate("2018-07-23T08:01:29.125Z"),
"title" : "Horse riding at EIRS",
"organisationFlag" : false,
"xperienceSubCategory" : [
ObjectId("5b4de8199f07bf0b303b8c5f")
],
"xpert" : [
ObjectId("5b51b054054a962cc6f914db")
],
"description" : "Beautiful rugged landscape makes Ladakh stand apart in the country. There is indeed so much that needs to be explored here and little did we know that amongst the best ways to explore this place is riding a horse. In the remote places like Zanskar Valley, where paved roads are rare and motor biking is not every ones cup of tea, horses make an incredible means of travel. One can access to Zangla, Penzela Pass, Panikhar Fort and Zongkhul, Stongdey and Gelugpa monasteries on a horseback.",
"__v" : 4,
"images" : [
{
"public_id" : "dotttg3xm0vvqygr4n1w",
"version" : 1537776979,
"signature" : "4fb562aa4115941e78f773dd11ed6be37a337acd",
"width" : 1280,
"height" : 640,
"format" : "jpg",
"resource_type" : "image",
"url" : "http://res.cloudinary.com/xplr-qa/image/upload/v1537776979/dotttg3xm0vvqygr4n1w.jpg",
"secure_url" : "https://res.cloudinary.com/xplr-qa/image/upload/v1537776979/dotttg3xm0vvqygr4n1w.jpg",
"_id" : ObjectId("5ba89d55ac69b4784a4f3a1f")
},
{
"public_id" : "cfdepst0r2wz39rowvzd",
"version" : 1537776980,
"signature" : "e770b7354c01ffce61c2e3cb83c79456dacc5438",
"width" : 2100,
"height" : 1526,
"format" : "jpg",
"resource_type" : "image",
"url" : "http://res.cloudinary.com/xplr-qa/image/upload/v1537776980/cfdepst0r2wz39rowvzd.jpg",
"secure_url" : "https://res.cloudinary.com/xplr-qa/image/upload/v1537776980/cfdepst0r2wz39rowvzd.jpg",
"_id" : ObjectId("5ba89d55ac69b4784a4f3a1e")
},
{
"public_id" : "lqfk6twitfefrjq1h2jq",
"version" : 1537776979,
"signature" : "ad1e0079a9419bde0047228446be17218117cf2e",
"width" : 2500,
"height" : 1666,
"format" : "jpg",
"resource_type" : "image",
"url" : "http://res.cloudinary.com/xplr-qa/image/upload/v1537776979/lqfk6twitfefrjq1h2jq.jpg",
"secure_url" : "https://res.cloudinary.com/xplr-qa/image/upload/v1537776979/lqfk6twitfefrjq1h2jq.jpg",
"_id" : ObjectId("5ba89d55ac69b4784a4f3a1d")
}
],
"source" : "",
"price" : 400,
"meetingLocation" : [],
"coordinates" : [
72.955162,
19.172098
],
"purchaseCount" : 10,
"Xpert" : [
{
"_id" : ObjectId("5b518101752982066a0dcc08"),
"name" : {
"last" : "Bourdain",
"first" : "Anthony"
}
},
{
"_id" : ObjectId("5b51b054054a962cc6f914db"),
"name" : {
"last" : "Yane",
"first" : "Sid"
}
},
{
"_id" : ObjectId("5b51c00597199d3ec4953e68"),
"name" : {
"last" : "ipsum",
"first" : "Lorem"
}
},
{
"_id" : ObjectId("5b59a8cdca722c3177cf3e63"),
"name" : {
"last" : "Lorem",
"first" : "Test"
}
},
{
"_id" : ObjectId("5b5eea6e2d4b6a04b2d9d599")
},
{
"_id" : ObjectId("5b5f02e22d4b6a04b2d9d5f1")
},
{
"_id" : ObjectId("5b60364f9e866d6453fef3ea"),
"name" : {
"last" : "Lorem",
"first" : "ipsum"
}
},
{
"_id" : ObjectId("5b6d70f96aaf19087ac6d9e9"),
"name" : {
"first" : "Indrajit",
"last" : "Jadhav"
}
},
{
"_id" : ObjectId("5b6d81953da5c50974642974"),
"name" : {
"last" : "Lorem",
"first" : "shin"
}
},
{
"_id" : ObjectId("5b6d831f3da5c50974642975"),
"name" : {
"last" : "Lorem",
"first" : "Shin "
}
},
{
"_id" : ObjectId("5b6d83b83da5c50974642976"),
"name" : {
"last" : "wert",
"first" : "fadsg"
}
},
{
"_id" : ObjectId("5b6d83ee3da5c50974642977"),
"name" : {
"last" : "asdfsaf",
"first" : "sadf"
}
},
{
"_id" : ObjectId("5b7ba46cd0e5fa137805552b"),
"name" : {
"first" : "Vivek",
"last" : "Pandey"
}
},
{
"_id" : ObjectId("5b7bb359d0e5fa13780555a9"),
"name" : {
"first" : "theamalageeks",
"last" : "null"
}
},
{
"_id" : ObjectId("5b7bd19ed0e5fa137805565d"),
"name" : {
"first" : "Anikesh",
"last" : "Baburanjan"
}
},
{
"_id" : ObjectId("5b7fa359211f5649518ae311"),
"name" : {
"first" : "Akhsay",
"last" : "Gaikwad"
}
},
{
"_id" : ObjectId("5b80181fab8747537011be5c"),
"name" : {
"first" : "Sagar",
"last" : "Bisen"
}
},
{
"_id" : ObjectId("5b83f1c5de663c0dcccfb483"),
"name" : {
"first" : "INDRAJIT",
"last" : "JADHAV"
}
},
{
"_id" : ObjectId("5b92154efc790f18121f9e02"),
"name" : {
"first" : "sagar",
"last" : "bisen"
}
},
{
"_id" : ObjectId("5ba39c509350135c9951ecb6"),
"name" : {
"first" : "Anikesh",
"last" : "Baburajan"
}
},
{
"_id" : ObjectId("5ba3b8e79350135c9951ecb7"),
"name" : {
"first" : "Anurag",
"last" : "Banerjee"
}
},
{
"_id" : ObjectId("5ba3c14e9350135c9951ecb8"),
"name" : {
"first" : "Dave",
"last" : "Jhala"
}
},
{
"_id" : ObjectId("5ba4dfcf6bb5277e1d7db2af"),
"name" : {
"first" : "Mark",
"last" : "Henry"
}
},
{
"_id" : ObjectId("5ba4e1c0452d7e01ce3dd34e"),
"name" : {
"first" : "Ankita",
"last" : "Mestry"
}
},
{
"_id" : ObjectId("5ba7795b452d7e01ce3dd34f"),
"name" : {
"first" : "Abhishek",
"last" : "Gupta"
}
},
{
"_id" : ObjectId("5ba9ec5844996a4be81a0c12"),
"name" : {
"first" : "Neeti",
"last" : "Patil"
}
},
{
"_id" : ObjectId("5baa1abf44996a4be81a0c13"),
"name" : {
"first" : "Krishna",
"last" : "S"
}
},
{
"_id" : ObjectId("5bb481d4c77dd914de2cb2f7"),
"name" : {
"first" : "krishna",
"last" : "shetty"
}
}
]
}
/* 2 */
{
"_id" : ObjectId("5bab23c897f85d07b9235a19"),
"updatedBy" : ObjectId("5b433dbf6a93e56a21f610eb"),
"updatedAt" : ISODate("2018-10-10T10:57:01.790Z"),
"createdBy" : ObjectId("5b433dbf6a93e56a21f610eb"),
"createdAt" : ISODate("2018-09-26T06:14:32.637Z"),
"xpert" : ObjectId("5ba4dfcf6bb5277e1d7db2af"),
"title" : "Mumbai Temples Tour",
"images" : [
{
"public_id" : "uvmemwfwmjfl7pmv4upk",
"version" : 1537943055,
"signature" : "5358320e4f396a2ee980e767a16d4a92228c95d6",
"width" : 1440,
"height" : 900,
"format" : "jpg",
"resource_type" : "image",
"url" : "http://res.cloudinary.com/xplr-qa/image/upload/v1537943055/uvmemwfwmjfl7pmv4upk.jpg",
"secure_url" : "https://res.cloudinary.com/xplr-qa/image/upload/v1537943055/uvmemwfwmjfl7pmv4upk.jpg",
"_id" : ObjectId("5bab260f97f85d07b9235a20")
},
{
"public_id" : "zztqwdbl7ejccxmougad",
"version" : 1537943055,
"signature" : "bcb413431d518b243d801f92ff9cf0a8f9bd7682",
"width" : 1920,
"height" : 1080,
"format" : "jpg",
"resource_type" : "image",
"url" : "http://res.cloudinary.com/xplr-qa/image/upload/v1537943055/zztqwdbl7ejccxmougad.jpg",
"secure_url" : "https://res.cloudinary.com/xplr-qa/image/upload/v1537943055/zztqwdbl7ejccxmougad.jpg",
"_id" : ObjectId("5bab260f97f85d07b9235a1f")
}
],
"organisationFlag" : false,
"xperienceSubCategory" : [
ObjectId("5b616b4c9e866d6453fef3eb")
],
"description" : "Global Vipassana Pagoda is an expression of our gratitude towards the Buddha who strived for incalculable aeons to reach Supreme Enlightenment.",
"__v" : 4,
"price" : 800,
"source" : "",
"meetingLocation" : [],
"coordinates" : [
72.955162,
19.172098
],
"primaryLocation" : ObjectId("5bacb0192e0b2751f0f7f057"),
"purchaseCount" : 20,
"Xpert" : [
{
"_id" : ObjectId("5b518101752982066a0dcc08"),
"name" : {
"last" : "Bourdain",
"first" : "Anthony"
}
},
{
"_id" : ObjectId("5b51b054054a962cc6f914db"),
"name" : {
"last" : "Yane",
"first" : "Sid"
}
},
{
"_id" : ObjectId("5b51c00597199d3ec4953e68"),
"name" : {
"last" : "ipsum",
"first" : "Lorem"
}
},
{
"_id" : ObjectId("5b59a8cdca722c3177cf3e63"),
"name" : {
"last" : "Lorem",
"first" : "Test"
}
},
{
"_id" : ObjectId("5b5eea6e2d4b6a04b2d9d599")
},
{
"_id" : ObjectId("5b5f02e22d4b6a04b2d9d5f1")
},
{
"_id" : ObjectId("5b60364f9e866d6453fef3ea"),
"name" : {
"last" : "Lorem",
"first" : "ipsum"
}
},
{
"_id" : ObjectId("5b6d70f96aaf19087ac6d9e9"),
"name" : {
"first" : "Indrajit",
"last" : "Jadhav"
}
},
{
"_id" : ObjectId("5b6d81953da5c50974642974"),
"name" : {
"last" : "Lorem",
"first" : "shin"
}
},
{
"_id" : ObjectId("5b6d831f3da5c50974642975"),
"name" : {
"last" : "Lorem",
"first" : "Shin "
}
},
{
"_id" : ObjectId("5b6d83b83da5c50974642976"),
"name" : {
"last" : "wert",
"first" : "fadsg"
}
},
{
"_id" : ObjectId("5b6d83ee3da5c50974642977"),
"name" : {
"last" : "asdfsaf",
"first" : "sadf"
}
},
{
"_id" : ObjectId("5b7ba46cd0e5fa137805552b"),
"name" : {
"first" : "Vivek",
"last" : "Pandey"
}
},
{
"_id" : ObjectId("5b7bb359d0e5fa13780555a9"),
"name" : {
"first" : "theamalageeks",
"last" : "null"
}
},
{
"_id" : ObjectId("5b7bd19ed0e5fa137805565d"),
"name" : {
"first" : "Anikesh",
"last" : "Baburanjan"
}
},
{
"_id" : ObjectId("5b7fa359211f5649518ae311"),
"name" : {
"first" : "Akhsay",
"last" : "Gaikwad"
}
},
{
"_id" : ObjectId("5b80181fab8747537011be5c"),
"name" : {
"first" : "Sagar",
"last" : "Bisen"
}
},
{
"_id" : ObjectId("5b83f1c5de663c0dcccfb483"),
"name" : {
"first" : "INDRAJIT",
"last" : "JADHAV"
}
},
{
"_id" : ObjectId("5b92154efc790f18121f9e02"),
"name" : {
"first" : "sagar",
"last" : "bisen"
}
},
{
"_id" : ObjectId("5ba39c509350135c9951ecb6"),
"name" : {
"first" : "Anikesh",
"last" : "Baburajan"
}
},
{
"_id" : ObjectId("5ba3b8e79350135c9951ecb7"),
"name" : {
"first" : "Anurag",
"last" : "Banerjee"
}
},
{
"_id" : ObjectId("5ba3c14e9350135c9951ecb8"),
"name" : {
"first" : "Dave",
"last" : "Jhala"
}
},
{
"_id" : ObjectId("5ba4dfcf6bb5277e1d7db2af"),
"name" : {
"first" : "Mark",
"last" : "Henry"
}
},
{
"_id" : ObjectId("5ba4e1c0452d7e01ce3dd34e"),
"name" : {
"first" : "Ankita",
"last" : "Mestry"
}
},
{
"_id" : ObjectId("5ba7795b452d7e01ce3dd34f"),
"name" : {
"first" : "Abhishek",
"last" : "Gupta"
}
},
{
"_id" : ObjectId("5ba9ec5844996a4be81a0c12"),
"name" : {
"first" : "Neeti",
"last" : "Patil"
}
},
{
"_id" : ObjectId("5baa1abf44996a4be81a0c13"),
"name" : {
"first" : "Krishna",
"last" : "S"
}
},
{
"_id" : ObjectId("5bb481d4c77dd914de2cb2f7"),
"name" : {
"first" : "krishna",
"last" : "shetty"
}
}
]
}
Instead of getting multiple Xperts in Xpert
Now I want to match the Xpert fields _id with xpert field of the output
Use the below aggregation.
Use $lookup pipeline variant. Let expression checks for a array type for xpert and converts into array when it is not. $match to collect all the matching users documents. $project to output name from user document.
db.uniquexperiences.aggregate([
{"$sample":{"size":2}},
{"$lookup":{
"from":"users",
"let":{"xpert":{"$cond":[{"$isArray":"$xpert"},"$xpert",["$xpert"]]}},
"pipeline":[
{"$match":{"$expr":{"$in":["$_id","$$xpert"]}}},
{"$project":{"name":1}}],
"as":"Xpert"
}}
])

Query Mongodb works with the terminal but not with node js

I find myself in front of a problem that I do not know how to solve.
The purpose of the query:
Compare the document of the selection person, and the one who requests the query if the id of the person selected is present in friend then is_friend will be equal to true otherwise it will be equal to false.
I got this query:
users.aggregate({
$match:{
search:/f/
}},{
$lookup:
{from:"users", let:{user:"$_id"}, pipeline:[{
$match:{
$expr:{
$and:[{
$in:["$$user", "$friend.id"]},{
$eq:["$_id", ObjectId("5bd22f28f77cfb1f6ce503ca")]
}]
}
}
},
{$limit:1},
{$project:{email:0, password:0}}
], as:"is_friend"}},{
$project:{name:1, search:1, desc:1, color:1, profil:1, banner:1, date:1, friend:1, is_friend:{
$cond:{
if:{
$eq:[{$arrayElemAt:["$is_friend",0]}, undefined]
},
then: "false",
else:"true"
}
}
}
})
So there is 1 document of users:
{
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"email" : "flarize.73#gmail.com",
"password" : "$2a$10$eYeOtEkEUyD7TFkjKvhZOuSSpvBolkL17TrPHuoHhOT8JrsQR0UKW",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd19a92da24674fdabd26b6"),
"date" : 1540676931288
},
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540676931288
}
]
}
When I call this request in the terminal i got this result:
{
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd19a92da24674fdabd26b6"),
"date" : 1540666689579
},
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540666689579
}
],
"is_friends" : "true"
}
That's the result I want.
But the node js i got this :
{
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd19a92da24674fdabd26b6"),
"date" : 1540666689579
},
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540666689579
}
],
is_friend : {
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd19a92da24674fdabd26b6"),
"date" : 1540676931288
},
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540676931288
}
]
}
}
How to solve the problem.
EDIT
mu node js code:
users.aggregate({
$match:{
search:new RegExp(req.body.search, 'i')
}},{
$lookup:
{from:"users", let:{user:"$_id"}, pipeline:[{
$match:{
$expr:{
$and:[{
$in:["$$user", "$friend.id"]},{
$eq:["$_id", new ObjectId(decoded["_id"])]
}]
}
}
},
{$limit:1},
{$project:{email:0, password:0}}
], as:"is_friend"}},{
$project:{name:1, search:1, desc:1, color:1, profil:1, banner:1, date:1, friend:1, is_friend:{
$cond:{
if:{
$eq:[{$arrayElemAt:["$is_friend",0]}, undefined]
},
then: "false",
else:"true"
}
}
}
}).toArray(function(err, result){
if(err) throw err;
ress.send(result);
});
Thank you for helping me

How to update an array of object in a document following unique value with mongoose

Giving the following controller:
MyCollection.findByIdAndUpdate(;
ObjectId(""+id+""),
{
$push: {"points":{
$each:[{long:long,
latitude:lat,
reportid:repid,
timestamp:timestamp,
$sort:{reportid:-1}
}}
},
{
safe:true, upsert:true, new:true
},
function(err, model){
console.log(err)
}
)
I initially have my collection like this:
"_id" : ObjectId("58a45fd8ddc4e1251055e197"),
"category" : "Others",
"name" : "edzs",
"points" : [
{
"timestamp" : "2017-03-07T15:57:30",
"repid" : "1609",
"latitude" : 45.644465,
"long" : 5.875074000000012
},
{
"timestamp" : "2017-03-07T14:59:30",
"repid" : "1608",
"latitude" : 45.644465,
"long" : 5.875074000000012
},
{
"timestamp" : "2017-03-07T14:20:30",
"repid" : "1606",
"latitude" : 45.644465,
"long" : 5.875074000000012
}
]
After every call by my controller, I got my collections like this:
"_id" : ObjectId("58a45fd8ddc4e1251055e197"),
"category" : "Others",
"name" : "edzs",
"points" : [
{
"timestamp" : "2017-03-07T16:30:30",
"repid" : "1610",
"latitude" : 45.654465,
"long" : 5.885074000000012
},
{
"timestamp" : "2017-03-07T15:57:30",
"repid" : "1609",
"latitude" : 45.644465,
"long" : 5.875074000000012
},
{
"timestamp" : "2017-03-07T15:57:30",
"repid" : "1609",
"latitude" : 45.644465,
"long" : 5.875074000000012
}
]
My objects inside "points" are cumulated sorted by reportid every call. How can I do to avoid duplicated "reportid" value. is there any commande line in mongoose permitting to controle reportid value in order to have unique in the array?

Using mongoose full text search on sub doc and return the match sub only

Hello i have proble searching sub document by text, i have been doing it right with query $text: {$search: "string"}, but it returns the whole document include the parent
this is my collection looks like
{
"_id" : ObjectId("56b06b7fe61af1081ceea185"),
"accountName" : "Agnes",
"address" : {
"street" : "jl. A.Yani no 23",
"city" : "Jakarta",
"province" : "DKI Jakarta",
"country" : "indonesia",
"postalCode" : "61382"
},
"__v" : NumberInt(1),
"products" : [
{
"title" : "Ayam goreng kremes",
"ingredients" : "Daging ayam, tepung terigu, bawang merah, sasa",
"description" : "",
"price" : "15000",
"_id" : ObjectId("56c191bd3da50ccc149717a5"),
"updatedAt" : ISODate("2016-02-15T08:52:13.465+0000"),
"createdAt" : ISODate("2016-02-15T08:52:13.465+0000"),
"showCounter" : NumberInt(0),
"featured" : false
},
{
"title" : "Sate Maranggi",
"ingredients" : "Daging sapi, kecap, cabai, bawang merah, bawang putih",
"description" : "Sate maranggi enak dan lezat",
"price" : "15000",
"_id" : ObjectId("56b06eae492acbbc1fd070e9"),
"pickOfTheDay" : false,
"sold" : NumberInt(0),
"featured" : true
}
]
}
{
"_id" : ObjectId("56c190203da50ccc149717a3"),
"accountName" : "acha",
"address" : {
"street" : "jl. Sudirman no 23",
"city" : "Bogor",
"province" : "Jawa Barat",
"country" : "indonesia",
"postalCode" : "61382"
},
"__v" : NumberInt(1)
"products" : [
{
"title" : "Pecel lele",
"ingredients" : "Lele, bawang merah, bawang putih, sasa, ketumbar, kemiri",
"description" : "",
"price" : "15000",
"_id" : ObjectId("56c192aa3da50ccc149717a9"),
"updatedAt" : ISODate("2016-02-15T08:56:10.716+0000"),
"createdAt" : ISODate("2016-02-15T08:56:10.716+0000"),
"showCounter" : NumberInt(0),
"featured" : false
}
]
}
and this is my search query
model.find({ $text: { $search: "Sate" } }).exec(function(err, docs){
if (err){
res.send(err);
}else{
res.send(docs);
}
and what i need is the sub document that matches the string "Sate" and the parent id if its possible.
maybe it looks like this
{
"parentID": ObjectId("56b06b7fe61af1081ceea185"),
"title" : "Sate Maranggi",
"ingredients" : "Daging sapi, kecap, cabai, bawang merah, bawang putih",
"description" : "Sate maranggi enak dan lezat",
"price" : "15000",
"_id" : ObjectId("56b06eae492acbbc1fd070e9"),
"pickOfTheDay" : false,
"sold" : NumberInt(0),
"featured" : true
}

Resources