I've implemented Solr and I am attempting to range query on a field and I am getting only half the expected results. Anyone have any ideas as to what might be going on here?
example:
myData = {
item1: {
my_field: 98
},
item2: {
my_field: 99
},
item3: {
my_field: 99
},
item4: {
my_field: 99
},
item5: {
my_field: 99
},
item6: {
my_field: 99
}
}
Range Queries:
[99 TO 99] // returns 1/5 expected results: item2
[99 TO 100] //returns 2/5: item2, item3
[99 TO 101] // returns 2/5: item2, item3
[99 TO 102] // Finally returns 5/5: item2, item3, item4, item5, item6
Related
Hello i have simple collection:
{
_id: 1,
books: [
{ bookId: 55, c: 5},
{ bookId: 66, c: 6},
{ bookId: 77, c: 7},
]
}
How i can add new field by calulate other field?
here i add field “Z” to current found object in nested array by it calculate field “C”
updateOne(
{
_id : 1,
'books.bookId' : 66
} ,
{
[
{ $addFields: { "books.$.z" : { "$sum" : ["$books.$.c", 1] } } }
]
}
Expected result:
{
_id: 1,
books: [
{ bookId: 55, c: 5},
{ bookId: 66, c: 6, z:7},
{ bookId: 77, c: 7},
]
}
I think there is a short entry (possibly using new $getField ?!), I think mango is still able to combine ‘position operator $’ + (‘varible operator reference by prefix $’ or ‘combine with $getField’) how i try in my sample
Use the aggregation pipeline in the update method to leverage the operators $map, $mergeObjects and $cond to achieve the desired results:
.updateOne(
// The query will find docs where *at least* one bookId in the
// books array equals 66 -- but remember it does not pinpoint
// the location in the array! We will tackle that in the update
// pipeline to follow...
{ _id: 1, 'books.bookId': 66 },
[
{ $set: {
books: {
$map: {
input: '$books',
in: {
$mergeObjects: [
'$$this',
{
$cond: [
{ $eq: ['$$this.bookId', 66] }, // IF books.bookId == 66
{ z: { $sum: ['$$this.c', 1] } }, // THEN merge a new field 'z' with calced value
null // ELSE merge null (a noop)
]
}
]
}
}
}
} }
]
)
I have a nested json data structure in mongodb which looks like:
{
'tid': 1,
'matches': [{
'dord': 1,
'matches': [{
'tord': 1,
'score': 11
},
{
'tord': 2,
'score': 12
}
]
},
{
'dord': 2,
'matches': [{
'tord': 1,
'score': 21
},
{
'tord': 2,
'score': 22
}
]
}]
}
I want to update the row with "dord": 1 and "tord": 1 and change value of score from 11 to 100. How do I do this?
What I already tried:
db.collection.update({'tid': 1}, {'matches': {$elemMatch: {'dord': 1}}}, {'matches': { $elemMatch: {'tord': 1}}}, {'score': 100})
Demo - https://mongoplayground.net/p/Mi2HnhzkPpE
https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/
The filtered positional operator $[] identifies the array elements that match the arrayFilters conditions for an update operation
db.collection.update({ "tid": 1 },
{ $set: { "matches.$[m].matches.$[t].score": 100 } },
{
arrayFilters: [
{ "m.dord": 1 }, // to match where dord = 1
{ "t.tord": 1, "t.score": 11 } // and where tord = 1 and score = 11
]
})
I have a collection structured like this (with sample data):
{
name: "Billie Jean",
active: false,
ranges: [
{ sfeMin: -6.75 , sfeMax: -7 , cilMin: 0.75 , cilMax: 0.75 },
{ sfeMin: -7.25 , sfeMax: -7.5 , cilMin: 1.25 , cilMax: 1.25 },
{ sfeMin: -7.5 , sfeMax: -7.75 , cilMin: 1.5 , cilMax: 1.5 },
{ sfeMin: -7.75 , sfeMax: -8 , cilMin: 1.75 , cilMax: 1.75 },
{ sfeMin: -6.5 , sfeMax: -6.75 , cilMin: 0.5 , cilMax: 0.5 },
{ sfeMin: -7 , sfeMax: -7.25 , cilMin: 0 , cilMax: 0 }
]
},
{
name: "Louis Armstrong",
active: true,
ranges: [
{ sfeMin: -8 ,sfeMax: 8 , cilMin: 0 , cilMax: 6 },
{ sfeMin: -8 ,sfeMax: 8 , cilMin: 0 , cilMax: 6 },
{ sfeMin: -8 ,sfeMax: 8 , cilMin: 0 , cilMax: 6 },
{ sfeMin: -8 ,sfeMax: 8 , cilMin: 0 , cilMax: 6 }
]
}
(this is some examples of data inserted into the collection).
What I need is to search if an item inside this collection is active and have a value, let's name it x, that falls between sfeMin and sfeMax and another value, let's name it y, that falls between cilMin and cilMax.
Having x=-8 and y=0, using this filter:
{ $and: [{ active: true }, { 'ranges.sfeMin': { $gte: -8 } }, { 'ranges.sfeMax': { $lte: -8 } }, { 'ranges.cilMin': { $gte: 0 } }, { 'ranges.cilMax': { $lte: 0 } } ]}
should not find any value.
But removing {active: false} or switching the value of all records to true for active, the query returns the collection's item with name "Billie Jean", which should not (at least in my desired behavior).
Let's work out the query step by step for the record in question:
all sfeMin are >= -8
the only sfeMax <= -8 is the fourth entry of the ranges array (-8)
for this row (with sfeMin = -7.75 and sfeMax= -8) cilMin is >=0 (in fact is 1.75) but cilMax is NOT <= 0 (again, 1.75).
Seems like MongoDB treat the ranges array as a single, flattened record instead of running the query in array's row by row. Considering the array as a single, big picture is OK to consider the query valid for Billie, because all values are present and correct but spanned in different array's rows.
This is not what I'm looking for.
The problem the way you've done it is the query will NOT know that ranges filters have to be part of the same object, it will look over all object of the array regardless there are in the same object.
Your query queries for documents where the ranges array has at least one embedded document that contains the field sfeMin gte to -8 and at least one embedded document (but not necessarily the same embedded document) that contains the field sfeMax equal to -8 and so on
I haven't tested it but I think this is the way it should be done:
{
active: true,
ranges: { $elemMatch: { sfeMin: { $gte: -8 }, sfeMax: { $lte: -8 }, cilMin: { $gte: 0 }, cilMax: { $lte: 0 } }}
}
If I have a response of this kind:
{
"A": 2,
"B": [
{
"CCC": "abcde",
"DDD": {
"EEE": 11,
"FFF": 22
}
},
{
"CCC": "fghij",
"DDD": {
"EEE": 111,
"FFF": 222
}
}
]
}
how can I get all the values CCC in a list or otherwise?
If I use:
.check(jsonPath("$..CCC").saveAs("VARIABLE"))
I only get the first CCC ("abcde"). Doing it via CCC[*] throws an error.
I think, you should have to add findAll in check statement.
for example:
.check(jsonPath("$..[*].CCC").findAll.saveAs("VARIABLE"))
And please define your error.
Cheers,
Peekay
I have a Model in MongoDB which include an array(category) which has an array(picture) inside it.
Shirt Model looks like this:
{ _id: 100, category: [ { name: 'CatName', _id: 101, picture: [Object] } ] }
And my Picture Array looks like this:
[ { path: 'p1', _id: 102 },
{ path: 'p2', _id: 103 } ] }
I Pass 3 variables
main array id which is 100 (var1)
category name which is CatName (var2)
picture id which is 102 (var3)
I want to GET an array which looks like:
{ _id: 100, category: [ { name: 'CatName', _id: 101,
picture: [ { path: 'p1', _id: 102 }]
} ]
}
What I have tried is this:
Shirt.find( {_id: var1} ,
{category: { $and:[ { name: var2 } , { picture: {$elemMatch: {_id: var3}}} ] }} )
.exec(function(err, product) {
console.log('Result ' + product );
res.jsonp(product);
});
But the Result I receive is Undefined for First Code
Second Code That I tried:
Shirt.find( {_id: var1} ,
{category: {$elemMatch: { name: var2,picture: {$elemMatch: {_id: var3} }}} } )
And Result from second code filter the array for var1 and var2
But it contain the whole picture array which means it does not filter var3
What is the correct code to find what I want?
Is this a correct approach for a Shopping Website database Or you have a better suggestion?
Am I applying Parent and Child Database Correctly?
Thanks!
Following mongo query will give you expected result:
db.collection.find({
"category": {
$elemMatch: {
"name": "CatName",
"picture": {
$elemMatch: {
"_id": 102
}
}
}
}
})
You need to convert it in node.js format.
Following format may help you. Not Tested
collection.find({
_id: "" // add your match Id here} ,
{
category: {
"$elemMatch": {
"name": "CatName",
"picture": {
$elemMatch: {
"_id": 102
}
}
}
}
}
})
.exec(function(err, product) {
console.log('Result ' + product);
res.jsonp(product);
});
AFTER EDIT QUESTION
You should use mongo aggregation to get required values. Query will be like following -
db.collection.aggregate({
$unwind: '$category'
}, {
$unwind: '$category.picture'
}, {
$match: {
_id: 100, // add here your var1 variable instead of 100
'category.name': 'CatName', // add here your var2 variable instead of 'CatName'
'category._id': 102, //add your match id here if any otherwise remove this condition
'category.picture._id': 102 //add your var3 instead of 102
}
}, {
$group: {
_id: '$category.name',
'name': {
$first: '$category.name'
},
'picture': {
'$push': '$category.picture'
}
}
})