Elasticsearch 6.2.4
Index has mapping:
{
"watcher" : {
"aliases" : { },
"mappings" : {
"doc" : {
"properties" : {
"script" : {
"properties" : {
"body" : {
"type" : "text"
},
"description" : {
"type" : "text"
},
"title" : {
"type" : "text"
}
}
},
"super-user" : {
"properties" : {
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"password" : {
"type" : "text"
},
"sha" : {
"type" : "text"
},
"username" : {
"type" : "text"
}
}
},
"watcher" : {
"properties" : {
"actions" : {
"type" : "object",
"enabled" : false
},
"condition" : {
"type" : "object",
"enabled" : false
}
}
}
}
}
}
}
}
There is a document I want to get by its _source.super-user.id value:
{
"_index" : "watcher",
"_type" : "doc",
"_id" : "sAkqs2UBN8hNgeAd6VYT",
"_score" : 1.0,
"_source" : {
"super-user" : {
"id" : "rwkTs2UBN8hNgeAd902q",
"username" : "elastic",
"sha" : "7598562076f37c7376ccf5c6ad28e00c:0fa96e2c4c0136b12ae1708940c46a52"
}
}
}
How do get this document?
I tried nested query:
const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({
host: [
{
host: 'localhost',
protocol: 'http',
auth: 'elastic:password',
port: 9200
}
]
});
(async () => {
try {
const resp = await client.search({
index: 'watcher',
type: 'doc',
body: {
query: {
nested: {
path: 'super-user',
query: {
bool: {
must: [
{
match: {
'super-user.id': 'rwkTs2UBN8hNgeAd902q'
}
}
]
}
}
}
}
}
});
console.log(JSON.stringify(resp, null, 2));
} catch (err) {
console.error(err);
}
})();
But I got failed to create query error:
{ Error: [query_shard_exception] failed to create query: {
"nested" : {
"query" : {
"bool" : {
"must" : [
{
"match" : {
"super-user.id" : {
"query" : "rwkTs2UBN8hNgeAd902q",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
"path" : "super-user",
"ignore_unmapped" : false,
"score_mode" : "avg",
"boost" : 1.0
}
}, with { index_uuid="5O9HfcORTjiq5SZ0c1lkQA" & index="watcher" }
at respond (/media/trex/safe/Development/private/node_modules/elasticsearch/src/lib/transport.js:307:15)
at checkRespForFailure (/media/trex/safe/Development/private/node_modules/elasticsearch/src/lib/transport.js:266:7)
at HttpConnector.<anonymous> (/media/trex/safe/Development/private/node_modules/elasticsearch/src/lib/connectors/http.js:159:7)
at IncomingMessage.bound (/media/trex/safe/Development/private/node_modules/elasticsearch/node_modules/lodash/dist/lodash.js:729:21)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
status: 400,
displayName: 'BadRequest',
message: '[query_shard_exception] failed to create query: {\n "nested" : {\n "query" : {\n "bool" : {\n "must" : [\n {\n "match" : {\n "super-user.id" : {\n "query" : "rwkTs2UBN8hNgeAd902q",\n "operator" : "OR",\n "prefix_length" : 0,\n "max_expansions" : 50,\n "fuzzy_transpositions" : true,\n "lenient" : false,\n "zero_terms_query" : "NONE",\n "auto_generate_synonyms_phrase_query" : true,\n "boost" : 1.0\n }\n }\n }\n ],\n "adjust_pure_negative" : true,\n "boost" : 1.0\n }\n },\n "path" : "super-user",\n "ignore_unmapped" : false,\n "score_mode" : "avg",\n "boost" : 1.0\n }\n}, with { index_uuid="5O9HfcORTjiq5SZ0c1lkQA" & index="watcher" }',
path: '/watcher/doc/_search',
query: {},
body:
{ error:
{ root_cause: [Array],
type: 'search_phase_execution_exception',
reason: 'all shards failed',
phase: 'query',
grouped: true,
failed_shards: [Array] },
status: 400 },
statusCode: 400,
response: '{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: {\\n \\"nested\\" : {\\n \\"query\\" : {\\n \\"bool\\" : {\\n \\"must\\" : [\\n {\\n \\"match\\" : {\\n \\"super-user.id\\" : {\\n \\"query\\" : \\"rwkTs2UBN8hNgeAd902q\\",\\n \\"operator\\" : \\"OR\\",\\n \\"prefix_length\\" : 0,\\n \\"max_expansions\\" : 50,\\n \\"fuzzy_transpositions\\" : true,\\n \\"lenient\\" : false,\\n \\"zero_terms_query\\" : \\"NONE\\",\\n \\"auto_generate_synonyms_phrase_query\\" : true,\\n \\"boost\\" : 1.0\\n }\\n }\\n }\\n ],\\n \\"adjust_pure_negative\\" : true,\\n \\"boost\\" : 1.0\\n }\\n },\\n \\"path\\" : \\"super-user\\",\\n \\"ignore_unmapped\\" : false,\\n \\"score_mode\\" : \\"avg\\",\\n \\"boost\\" : 1.0\\n }\\n}","index_uuid":"5O9HfcORTjiq5SZ0c1lkQA","index":"watcher"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"watcher","node":"OHtckm41Ts2DwDlT0A7N4w","reason":{"type":"query_shard_exception","reason":"failed to create query: {\\n \\"nested\\" : {\\n \\"query\\" : {\\n \\"bool\\" : {\\n \\"must\\" : [\\n {\\n \\"match\\" : {\\n \\"super-user.id\\" : {\\n \\"query\\" : \\"rwkTs2UBN8hNgeAd902q\\",\\n \\"operator\\" : \\"OR\\",\\n \\"prefix_length\\" : 0,\\n \\"max_expansions\\" : 50,\\n \\"fuzzy_transpositions\\" : true,\\n \\"lenient\\" : false,\\n \\"zero_terms_query\\" : \\"NONE\\",\\n \\"auto_generate_synonyms_phrase_query\\" : true,\\n \\"boost\\" : 1.0\\n }\\n }\\n }\\n ],\\n \\"adjust_pure_negative\\" : true,\\n \\"boost\\" : 1.0\\n }\\n },\\n \\"path\\" : \\"super-user\\",\\n \\"ignore_unmapped\\" : false,\\n \\"score_mode\\" : \\"avg\\",\\n \\"boost\\" : 1.0\\n }\\n}","index_uuid":"5O9HfcORTjiq5SZ0c1lkQA","index":"watcher","caused_by":{"type":"illegal_state_exception","reason":"[nested] nested object under path [super-user] is not of nested type"}}}]},"status":400}',
toString: [Function],
toJSON: [Function] }
nested is a specific data type: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
In your mapping, you dont specify it, so use the simple dot notation to select the right field without the nested query.
Related
I have 3 different collections which has build relation with the another field then primary id.
For ex, I have users, educations and user profiles 3 different collection and users and educations have relation with key user_id of user and same for userinfo. please find sample data.
let users = [
{ _id: ObjectId("1"), user_id: 1, name: "Nitin", is_active: true, is_deleted: false },
{ _id: ObjectId("2"), user_id: 2, name: "Vishal", is_active: true, is_deleted: false },
]
let educations =
[
{ _id: ObjectId("33"), user: 1, std: "10", is_active: true, is_deleted: false },
{ _id: ObjectId("44"), user: 2, std: "8", is_active: true, is_deleted: false },
]
let userinfo = [
{ _id: ObjectId("33"), user: 1, weight: "50", is_active: true, is_deleted: false },
{ _id: ObjectId("44"), user: 2, weight: "45", is_active: true, is_deleted: false },
]
Now I would like to fetch the relation data and created query by using $lookup by following way.
let criteria = {
is_active: true,
is_deleted: false,
};
const result = await mongoose.connection.db.collection('users').aggregate([
//education
{ $lookup: { from: "education", localField: "user_id", foreignField: "user", as: "education" } },
{ $unwind: { path: "$education", preserveNullAndEmptyArrays: true } },
//userinfo
{ $lookup: { from: "userinfo", localField: "user_id", foreignField: "user", as: "userinfo" } },
{ $unwind: { path: "$userinfo", preserveNullAndEmptyArrays: true } },
{ $match: { is_deleted: false } },
{ $match: criteria },
{
"$facet": {
"totalLocation": [
{ $match: criteria },
{ "$count": "count" },
],
}
},
{
"$project": {
"totalLocation": { "$arrayElemAt": ["$totalLocation.count", 0] },
}
}
], { allowDiskUse: true, collation: { locale: 'en_US', alternate: "shifted" } }).toArray();
This query works completely fine and return data as expected. But now as data are growing so this query became slower and we would make it faster. one of solution which I found that to create an index in a way so we can have faster result. I have tried but it doesn't works for me
so can anyone help me to create a index on this kind of query. or any another solution.
Explanation as requested in comment
{
"stages" : [
{
"$cursor" : {
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "mydata.users",
"indexFilterSet" : false,
"parsedQuery" : {
"is_deleted" : {
"$eq" : false
}
},
"queryHash" : "242D9E6F",
"planCacheKey" : "386E80BB",
"winningPlan" : {
"stage" : "PROJECTION_SIMPLE",
"transformBy" : {
"cb_id" : 1,
"user" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "COLLSCAN",
"filter" : {
"is_deleted" : {
"$eq" : false
}
},
"direction" : "forward"
}
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2328,
"executionTimeMillis" : 265,
"totalKeysExamined" : 0,
"totalDocsExamined" : 2355,
"executionStages" : {
"stage" : "PROJECTION_SIMPLE",
"nReturned" : 2328,
"executionTimeMillisEstimate" : 10,
"works" : 2357,
"advanced" : 2328,
"needTime" : 28,
"needYield" : 0,
"saveState" : 3,
"restoreState" : 3,
"isEOF" : 1,
"transformBy" : {
"cb_id" : 1,
"user" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "COLLSCAN",
"filter" : {
"is_deleted" : {
"$eq" : false
}
},
"nReturned" : 2328,
"executionTimeMillisEstimate" : 0,
"works" : 2357,
"advanced" : 2328,
"needTime" : 28,
"needYield" : 0,
"saveState" : 3,
"restoreState" : 3,
"isEOF" : 1,
"direction" : "forward",
"docsExamined" : 2355
}
}
}
},
"nReturned" : NumberLong(2328),
"executionTimeMillisEstimate" : NumberLong(20)
},
{
"$lookup" : {
"from" : "education",
"as" : "education",
"localField" : "user_id",
"foreignField" : "user",
"unwinding" : {
"preserveNullAndEmptyArrays" : true
}
},
"nReturned" : NumberLong(2328),
"executionTimeMillisEstimate" : NumberLong(136)
},
{
"$lookup" : {
"from" : "userinfo",
"as" : "userinfo",
"localField" : "user_id",
"foreignField" : "user",
"unwinding" : {
"preserveNullAndEmptyArrays" : true
}
},
"nReturned" : NumberLong(2328),
"executionTimeMillisEstimate" : NumberLong(264)
},
{
"$facet" : {
"totalLocation" : [
{
"$teeConsumer" : {},
"nReturned" : NumberLong(2328),
"executionTimeMillisEstimate" : NumberLong(264)
},
{
"$group" : {
"_id" : {
"$const" : null
},
"count" : {
"$sum" : {
"$const" : 1
}
}
},
"nReturned" : NumberLong(1),
"executionTimeMillisEstimate" : NumberLong(264)
},
{
"$project" : {
"count" : true,
"_id" : false
},
"nReturned" : NumberLong(1),
"executionTimeMillisEstimate" : NumberLong(264)
}
]
},
"nReturned" : NumberLong(1),
"executionTimeMillisEstimate" : NumberLong(264)
},
{
"$project" : {
"_id" : true,
"totalLocation" : {
"$arrayElemAt" : [
"$totalLocation.count",
{
"$const" : 0.0
}
]
}
},
"nReturned" : NumberLong(1),
"executionTimeMillisEstimate" : NumberLong(264)
}
],
"serverInfo" : {},
"ok" : 1.0
}
Thanks
my collection Stucture is like below:
{"_id" : ObjectId("5aec2ce5020ba15d2fb2665a"),
"password" : "e10adc3949ba59abbe56e057f20f883e",
"unique_id" : 22,
"first_name" : "Foram",
"last_name" : "Test ",
"country_phone_code" : "+61",
"email" : "a#a.com",
"phone" : "1231231231",
"device_type" : "android",
"admintypeid" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"vehicle_detail" : [
{
"service_name" : "You",
"_id" : ObjectId("5aec2d08020ba15d2fb2665c"),
"name" : "Qee",
"plate_no" : "Qwe111",
"model" : "Qee",
"color" : "Blue",
"passing_year" : "2005",
"service_type" : [
ObjectId("5a9f9c53b9e8fa66f9b934c1")
],
"admin_type_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"is_selected" : true,
"pictureData" : "vehicle_images/5aec2d08020ba15d2fb2665cWjyP.jpg"
},
{
"pictureData" : "vehicle_images/5aec35e00efc106080e14ec7y2tO.jpg",
"is_selected" : false,
"admin_type_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"service_type" : [
ObjectId("5a9f9c53b9e8fa66f9b934c1"),
ObjectId("5ac954b7075e16583dc3311f")
],
"passing_year" : "2018",
"color" : "No",
"model" : "Bcs",
"plate_no" : "12112",
"name" : "Bcs",
"_id" : ObjectId("5aec35e00efc106080e14ec7"),
"service_name" : "You"
}
],
"service_type" : [
ObjectId("5a9f9c53b9e8fa66f9b934c1")
],
"is_vehicle_document_uploaded" : true,
"is_trip" : [],
"__v" : 14
}
i want to campare ids in my service_type array which is in vehicle_detail array
to another collection named city_types than from that i have to comapare typid of city_types collection to tyes collection and get value of name from type collection
my city_type collection would be something like below
{
"_id" : ObjectId("5a9f9c53b9e8fa66f9b934c1"),
"countryid" : ObjectId("5abb275fd20731597cc01229"),
"cityid" : ObjectId("5abb27cbd20731597cc0122a"),
"typeid" : ObjectId("5a9f9b55b9e8fa66f9b934c0")
"__v" : 0}
containing type_id
and my types collection is like below
{
"_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"typename" : "You",
"description" : "KARRU You",
"__v" : 0,
"main_type" : 0
}
what i want it name of type
from id
i have used code below:
exports.get_provider_vehicle_list = function (req, res, next) {
var mongoose = require('mongoose');
var Schema = mongoose.Types.ObjectId;
var condition = {$match: {"_id": Schema(req.body.provider_id)}};
var vunwind = {$unwind: "$vehicle_detail"}
var lookup = {
$lookup:
{
from: "types",
localField: "vehicle_detail.admin_type_id",
foreignField: "_id",
as: "type_detail"
}
};
var unwind = {$unwind: {
path: "$type_detail",
preserveNullAndEmptyArrays: true
}
};
var group = {$group: {
_id: null,
"vehicle_detail": {$push: {
is_selected: "$vehicle_detail.is_selected",
admin_type_id: "$vehicle_detail.admin_type_id",
service_type: "$vehicle_detail.service_type",
passing_year: "$vehicle_detail.passing_year",
color: "$vehicle_detail.color",
model: "$vehicle_detail.model",
plate_no: "$vehicle_detail.plate_no",
name: "$vehicle_detail.name",
_id: "$vehicle_detail._id",
type_image_url: '$type_detail.type_image_url',
service_name:"$vehicle_detail.service_name",
pictureData:"$vehicle_detail.pictureData"
}}
}
}
Provider.aggregate([condition, vunwind, lookup, unwind, group], function (err, provider) {
if (err || provider.length == 0) {
res.json({success: true, vehicle_list: []})
} else {
res.json({success: true, vehicle_list: provider[0].vehicle_detail})
}
})
};
and my res is like below:
{
"success": true,
"vehicle_list": [
{
"is_selected": true,
"admin_type_id": "5a9f9b55b9e8fa66f9b934c0",
"service_type": [
"5a9f9c53b9e8fa66f9b934c1",
"5ac954b7075e16583dc3311f"
],
"passing_year": "2005",
"color": "Blue",
"model": "Qee",
"plate_no": "Qwe111",
"name": "Qee",
"_id": "5aec2d08020ba15d2fb2665c",
"type_image_url": "service_type_images/5a9f9b55b9e8fa66f9b934c0mjYA.png",
"service_name": "You",
"pictureData": "vehicle_images/5aec2d08020ba15d2fb2665cWjyP.jpg"
}
]
}
and res i want is below
{
"success": true,
"vehicle_list": [
{
"is_selected": true,
"admin_type_id": "5a9f9b55b9e8fa66f9b934c0",
"service_type": [
"5a9f9c53b9e8fa66f9b934c1",
"5ac954b7075e16583dc3311f"
],
"passing_year": "2005",
"color": "Blue",
"model": "Qee",
"plate_no": "Qwe111",
"name": "Qee",
"_id": "5aec2d08020ba15d2fb2665c",
"type_image_url": "service_type_images/5a9f9b55b9e8fa66f9b934c0mjYA.png",
"service_type_name": [
"You"
],
"service_name": "You",
"pictureData": "vehicle_images/5aec2d08020ba15d2fb2665cWjyP.jpg"
}
]
}
please check.
db.getCollection('vehicle').aggregate([
{
$unwind: {
path: '$vehicle_detail',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$vehicle_detail.service_type',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'city_type',
localField: 'vehicle_detail.service_type',
foreignField: '_id',
as: 'city_type',
},
},
{
$unwind: {
path: '$city_type',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'types',
localField: 'city_type.typeid',
foreignField: '_id',
as: 'types',
},
},
{
$unwind: {
path: '$types',
preserveNullAndEmptyArrays: false,
},
},
{
$group: {
_id: '$_id',
first_name: { $first: 1 },
last_name: { $first: 1 },
types: { $first: '$types' },
},
},
])
Result
{
"_id" : ObjectId("5aec2ce5020ba15d2fb2665a"),
"first_name" : 1.0,
"last_name" : 1.0,
"types" : {
"_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"typename" : "You",
"description" : "KARRU You",
"__v" : 0,
"main_type" : 0
}
}
Note : You can added fields in group which you need.
I am sending what I understood from your explanation. I hope it helps.
Some points:
I changed your objects city_types and types to an array, since you said Collections;
I link the id to match Vehicle->city->types
function ObjectId(id){
return id;
}
var obj = {"_id" : ObjectId("5aec2ce5020ba15d2fb2665a"),
"password" : "e10adc3949ba59abbe56e057f20f883e",
"unique_id" : 22,
"first_name" : "Foram",
"last_name" : "Test ",
"country_phone_code" : "+61",
"email" : "a#a.com",
"phone" : "1231231231",
"device_type" : "android",
"admintypeid" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"vehicle_detail" : [
{
"service_name" : "Service 1",
"_id" : ObjectId("5aec2d08020ba15d2fb2665c"),
"name" : "Qee",
"plate_no" : "Qwe111",
"model" : "Qee",
"color" : "Blue",
"passing_year" : "2005",
"service_type" : [
ObjectId("5a9f9c53b9e8fa66f9b934c1")
],
"admin_type_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"is_selected" : true,
"pictureData" : "vehicle_images/5aec2d08020ba15d2fb2665cWjyP.jpg"
},
{
"pictureData" : "vehicle_images/5aec35e00efc106080e14ec7y2tO.jpg",
"is_selected" : false,
"admin_type_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"service_type" : [
ObjectId("5a9f9c53b9e8fa66f9b934c1"),
ObjectId("5ac954b7075e16583dc3311f")
],
"passing_year" : "2018",
"color" : "No",
"model" : "Bcs",
"plate_no" : "12112",
"name" : "Bcs",
"_id" : ObjectId("5aec35e00efc106080e14ec7"),
"service_name" : "Service 2"
}
],
"service_type" : [
ObjectId("5a9f9c53b9e8fa66f9b934c1")
],
"is_vehicle_document_uploaded" : true,
"is_trip" : [],
"__v" : 14
};
var city_types = [{
"_id" : ObjectId("5aec2d08020ba15d2fb2665c"),
"countryid" : ObjectId("5abb275fd20731597cc01229"),
"cityid" : ObjectId("5abb27cbd20731597cc0122a"),
"typeid" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"__v" : 0
}];
var types = [{
"_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"typename" : "You",
"description" : "KARRU You",
"__v" : 0,
"main_type" : 0
}];
function print() {
//iterate over vehicle_detail
obj.vehicle_detail.forEach((vehicle)=>{
console.log("vehicle="+ vehicle._id);
//iterate over cities type
city_types.forEach((city)=> {
console.log("city="+ city._id);
if (city._id == vehicle._id) {
//iterate over types
types.forEach((type)=> {
console.log("type="+ type._id);
if (city.typeid == type._id) {
console.log("Type Name: "+type.typename);
}
})
}
})
});
}
print();
The output of the program is:
>node index4.js
vehicle=5aec2d08020ba15d2fb2665c
city=5aec2d08020ba15d2fb2665c
type=5a9f9b55b9e8fa66f9b934c0
Type Name: You
vehicle=5aec35e00efc106080e14ec7
city=5aec2d08020ba15d2fb2665c
I am using mongodb database, I unable to apply query to fetch all document with status CERTIFIED,I am using below document, please suggets any query like to fetch all document with "status":"CERTIFIED"
{
"_id" : ObjectId("592fb37d57693963cca325e5"),
"json" : {
"query_by_gtin_response:queryByGtinResponse" : {
"productData" : {
"gtin" : "31212121212167",
"productDataRecord" : {
"module" : [
{
"bpi:basicProductInformationModule" : {
"xmlns:bpi" : "urn:gs1:tsd:basic_product_information_module:xsd:1",
"productName" : {
"languageCode" : "en",
"value" : "test"
},
"brandNameInformation" : {
"brandName" : {
"value" : "test pub"
}
}
}
},
{
"product_tracking_information_module" : {
"variantId" : {
"value" : "31124ff0-4693-11e7-9492-01c27f5bdb71"
},
"status" : {
"value" : "CERTIFIED"
},
"name" : {
"value" : "admin#gmail.com"
},
"createdBy" : {
"value" : "552f5b90b348147e03e49b62"
},
"createdDate" : {
"value" : "2017-06-01T06:26:05.551Z"
},
"updatedBy" : {
"value" : "552f5b90b348147e03e49b62"
},
"updatedDate" : {
"value" : "2017-06-01T06:26:05.551Z"
},
"applicationId" : {
"value" : "webapp"
},
"history" : {
"createdDate" : {
"value" : "2017-06-01T06:26:05.551Z"
},
"updatedDate" : {
"value" : "2017-06-01T06:26:05.551Z"
},
"status" : {
"value" : "CERTIFIED"
},
"updatedBy" : {
"value" : "552f5b90b348147e03e49b62"
},
"createdBy" : {
"value" : "552f5b90b348147e03e49b62"
},
"applicationId" : {
"value" : "webapp"
},
"name" : {
"value" : "admin#gmail.com"
}
}
}
}
]
}
}
}
}
}
You can use a function as your $where operator:
collection.find({ $where: function() {
var productDataRecord = this.json["query_by_gtin_response:queryByGtinResponse"]["productData"]["productDataRecord"];
var status = productDataRecord["module"][1]["product_tracking_information_module"].status;
return (status.value == "CERTIFIED")
}})
Below is my update code:
async.each(jsondata,
function(itemdata, callbackNew){
itemdata.store_code=parseInt(itemdata.store_code);
//console.log(itemdata.store_code);
db.mdb.collection('counters')
.update(
{"store_code": itemdata.store_code},{$set:itemdata},
{ upsert: true },
function (erreach, data) {
if (erreach) {
console.log("error reported")
console.log(erreach)
callbackNew(erreach);
}
else{
//console.log('Data updated')
callbackNew();
app.send(req,res,data);
}
})
},function(err){
if(err) {
//console.log("this is the error"+err)
app.senderr(req,res,err);
}
else{
app.send(req,res,jsondata);
}
});
But the DB is not getting changed.The values are same before and after updation.
Below is the JSON of my database.
{
"_id" : ObjectId("586aac4c8231ee0b98458045"),
"store_code" : NumberInt(10800),
"counter_name" : "R.N.Electric",
"address" : "314 khatipura road",
"locality" : "Khatipura Road (Jhotwara)",
"pincode" : "302012",
"town" : "JAIPUR",
"gtm_city" : "JAIPUR",
"sales_office" : "URAJ",
"owner_name" : "Rajeev",
"owner_mobile" : "9828024073",
"division_mapping" : [
{
"dvcode" : "cfc",
"dc" : "trade",
"beatcode" : "govindpura",
"fos" : {
"_id" : ObjectId("586ab8318231ee0b98458843"),
"loginid" : "9928483483",
"name" : "Arpit Gupta",
"division" : [
"cfc",
"iron"
],
"sales_office" : "URAJ",
"gtm_city" : "JAIPUR"
},
"beat" : {
"_id" : ObjectId("586d372b39f64316b9c3cbd7"),
"division" : {
"_id" : ObjectId("5869f8b639f6430fe4edee2a"),
"clientdvcode" : NumberInt(40),
"code" : "cfc",
"name" : "Cooking & Fabric Care",
"project_code" : "usha-fos",
"client_code" : "usha",
"agent_code" : "v5global"
},
"beatcode" : "govindpura",
"sales_office" : "URAJ",
"gtm_city" : "JAIPUR",
"active" : true,
"agency_code" : "v5global",
"client_code" : "USHA_FOS",
"proj_code" : "usha-fos",
"fos" : {
"_id" : ObjectId("586ab8318231ee0b98458843"),
"loginid" : "9928483483",
"name" : "Arpit Gupta",
"division" : [
"cfc",
"iron"
],
"sales_office" : "URAJ",
"gtm_city" : "JAIPUR"
}
}
}
],
"distributor_mail" : "sunil.todi#yahoo.in",
"project_code" : "usha-fos",
"client_code" : "usha",
"agent_code" : "v5global",
"distributor_name" : "Sundeep Electrical"
}
And below is the document which I am sending in update($set:itemdata):
{ store_code: '10800',
counter_name: 'R.N.Electrics',
address: '314 khatipura road',
locality: 'Khatipura Road (Jhotwara)',
pincode: '302012',
town: 'JAIPUR',
gtm_city: 'JAIPUR',
sales_office: 'URAJ',
owner_name: 'Rajeev',
owner_mobile: '9828024073',
distributor_mail: 'sunil.todi#yahoo.in',
distributor_name: 'Sundeep Electrical' }
How can I check if my aggregate query is good or bad? I already use "explain" but it's not specific.
Here's the output of my aggregate "explain".
{
"waitedMS" : NumberLong(0),
"stages" : [
{
"$cursor" : {
"query" : {
"keys.license_id" : ObjectId("580eeb7fb79bec95648775a2"),
"deleted.status" : {
"$ne" : 1
}
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "serpentsmsapp.conversations",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"keys.license_id" : {
"$eq" : ObjectId("580eeb7fb79bec95648775a2")
}
},
{
"$not" : {
"deleted.status" : {
"$eq" : 1
}
}
}
]
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [
{
"keys.license_id" : {
"$eq" : ObjectId("580eeb7fb79bec95648775a2")
}
},
{
"$not" : {
"deleted.status" : {
"$eq" : 1
}
}
}
]
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
}
}
},
{
"$sort" : {
"sortKey" : {
"_id" : 1,
"keys.license_id" : 1,
"status.deleted" : 1
}
}
},
{
"$lookup" : {
"from" : "conversation_messages",
"as" : "cmf",
"localField" : "_id",
"foreignField" : "keys.conv_id",
"unwinding" : {
"preserveNullAndEmptyArrays" : false
}
}
},
{
"$match" : {
"cmf.deleted.status" : 0
}
},
{
"$sort" : {
"sortKey" : {
"cmf.deleted.status" : -1
}
}
},
{
"$group" : {
"_id" : {
"id" : "$_id",
"number" : "$number",
"mode" : "$mode",
"keys" : "$keys",
"ports" : "$ports",
"user_assign" : "$user_assign",
"spam" : "$spam"
},
"cm_field" : {
"$last" : {
"id" : "$cmf._id",
"message" : "$cmf.message",
"ports" : "$cmf.ports",
"mode" : "$cmf.mode",
"keys" : "$cmf.keys",
"status" : "$cmf.status",
"date" : "$cmf.updated"
}
},
"counts" : {
"$sum" : "$cmf.status"
},
"sms_mode" : {
"$addToSet" : "$cmf.mode"
}
}
},
{
"$sort" : {
"sortKey" : {
"cm_field.date" : -1
},
"limit" : NumberLong(5000)
}
},
{
"$group" : {
"_id" : "$_id",
"cm_field" : {
"$last" : "$cm_field"
},
"counts" : {
"$first" : "$counts"
},
"sms_mode" : {
"$first" : "$sms_mode"
}
}
}
],
"ok" : 1
}
How can I see how many docs are scanned before performing my query?
In order to solve your problem I suggest you the following plan
Learn how does Explain work.
"Good or bad" approach is not an engineering approach, it always depends on a lot of factors (requirements, hardware, etc...). You should define what are those requirements before answering if it query is performant enough.
For your specific explain log. "COLSCAN" - means it has used all your documents on the collection to aggregate the result, this usually is an alert sign, you should think of using proper indexes.