Get index of the last matching element of an array in MongoDB - javascript

Is there a way to get the index of the last matching element in MongoDB? I'm speaking of an equivalent to JavaScript's Array.prototype.lastIndexOf. I know about $indexOfArray, but I'm looking for the last index, not the first one.
My use case
I've got a collection whose schema looks like this:
{
// ...
images: [
{
identifier: String,
imageId: ObjectId
}
]
// ...
}
I have a record whose got duplicated elements:
{
// ...
images: [
{
identifier: '0',
imageId: ObjectId('objectId0')
},
{
identifier: '1',
imageId: ObjectId('objectId1')
},
{
identifier: '2',
imageId: ObjectId('objectId2')
},
{
identifier: '0', // <-- duplicated!
imageId: ObjectId('objectId3')
},
// many more elements...
{
identifier: '0', // last occurence
imageId: ObjectId('objectIdN+0')
},
{
identifier: '1',
imageId: ObjectId('objectIdN+1')
},
{
identifier: '2',
imageId: ObjectId('objectIdN+2')
}
]
}
I want to remove all the elements before the last occurence of images.identifier: '0'.
Is it possible at all without the usage of JavaScript?

Try this one:
db.collection.aggregate([
{
$set: {
images: {
$reduce: {
input: "$images",
initialValue: [],
in: {
$cond: {
if: { $eq: ["$$this.identifier", "0"] },
then: ["$$this"], // reset array to current value
else: { $concatArrays: ["$$value", ["$$this"]] } // append values
}
}
}
}
}
}
])

Related

How do I pick then sort MongoDB documents and then find next document to mine?

I have a collection of documents, which I need to first narrow down by set criteria, then sort alphabetically by string value inside those documents — let's say that's a "search result". I then need to find document that matches a given _id and then pick a document next to it (before or after) from the above "search result".
Background:
I use mongoose to query my database via Node.js.
I have a set of "special sections" in my blog that are comprised of all the articles that must have three particular conditions associated within the keys in the document. I can get the list of articles belonging to said section like so:
const specialSectionListQuery = Article.find({
tag: { $ne: "excluded" },
[`collections.cameras`]: { $exists: true },
status: "published",
})
To finish creating the "special section," I must sort the documents alphabetically via their title attribute:
.sort({ [`collections.cameras.as.title`]: "asc" })
Now I want to add a link to "next article within the same special section" at the bottom of such articles. I know _id and any other value needed from the current article. The above query gives me an ordered list of documents within the section so I can easily find it within that list specialSectionListQuery.findOne({ _id: "xxx" }).exec().
However, I need to find the next article within the above list. How do I do that?
My attempts thus far:
I tried to create article list via aggregation, which led me nowhere (I simply made my app do exactly the same thing — make a list for a "special sectin"):
Article.aggregate([
{
$match: {
tag: { $ne: "excluded" },
[`collections.cameras`]: { $exists: true },
status: "published",
},
},
{
$sort: {
[`collections.cameras.as.title`]: 1,
},
}
]).exec()
But I can't for the life of me figure out how to iterate to the next document in the list properly.
I have thought of saving the list in Node.js memory (variable) and then finding what I need via JavaScript but that can't be scalable.
I have considered creating a new collection and saving the above list there but that would require me to either 1) do it every time a document is altered/added/deleted via Node.js — which is a lot of code and it may break if I interact with database another way 2) rebuild the colleciton every time I run the query, but that feels like it'll lack in performance.
Please help and thank you!
P.S.:
Example collection which should cover most of the cases I'm looking to solve for:
[
{
_id: 1,
name: "Canon",
collections: { cameras: { as: { title: "Half-Frame" } } },
tag: "included",
status: "published"
},
{
_id: 2,
name: "Pentax",
collections: { cameras: { as: { title: "Full-Frame" } } },
tag: "included",
status: "published"
},
{
_id: 3,
name: "Kodak",
collections: { film: { as: { title: "35mm Film" } } },
tag: "included",
status: "published"
},
{
_id: 4,
name: "Ricoh",
collections: { cameras: { as: { title: "Full-Frame" } } },
tag: "included",
status: "published"
},
{
_id: 5,
name: "Minolta",
collections: { cameras: { as: { title: "Half-Frame Review" } } },
tag: "excluded",
status: "published"
},
{
_id: 4,
name: "FED",
collections: { cameras: { as: { title: "Full-Frame" } } },
tag: "included",
status: "draft"
}
]
One thing you can try is to extend your $sort by adding _id so that it always returns documents in deterministic order:
{
$sort: {
"collections.cameras.as.title": 1,
_id: 1
}
},
{
$limit: 1
}
Once your first query returns the document with _id: 2 and collections.cameras.as.title: Full-Frame, you can use below query to get subsequent document:
{
$match: {
$and: [
{
tag: { $ne: "excluded" },
"collections.cameras": { $exists: true },
status: "published",
},
{
$or: [
{
$and: [
{ "collections.cameras.as.title": { $eq: "Full-Frame" } },
{ "_id": { $gt: 2 } }
]
},
{ "collections.cameras.as.title": { $gt: "Full-Frame" } }
]
}
]
}
},
{
$sort: {
"collections.cameras.as.title": 1,
_id: 1
}
},
{
$limit: 1
}
In this case due to deterministic $sort you can exclude previously found document by adding additional filtering criteria and the order should be preserved.
Mongo Playground

Reduce mongodb aggregation with condition

I have an array of objects call "extra" with different properties: some objects have "plus" and some haven't.
I want to create inside this "extra" array, 2 different arrays one called "cheap" with all the object that don't have the "plus" property and one called "exp" with only the objects with the "plus" property.
I think I can use the $reduce method in mongodb aggregate with $concatArrays and check with $cond if the property plus exists or not.
Something like that:
Data example:
{
extra: [
{
description: "laces",
type: "exterior",
plus: '200'
},
{
description: "sole",
type: "interior"
},
{
description: "logo",
type: "exterior"
},
{
description: "stud",
type: "exterior",
plus: '450'
}
],
}
{
$project: {
extra: {
$reduce: {
input: ['$extra'],
initialValue: {cheap: [], exp: []},
$cond: {
if: {$eq: ['$$this.plus', null]},
then: {
in: {
cheap: {
$concatArrays: ['$$value.cheap', '$$this'],
},
},
},
else: {
in: {
exp: {
$concatArrays: ['$$value.exp', '$$this'],
},
},
},
},
},
},
},
}
It doesn't work...I tried many ways or writing the $cond part without luck.
I can't figure it out.
Thank you all.
K.
Apart from some minor syntax issues you've had another problem is your understand of the $ne operator.
In this case you expect a missing value to be equal to null, this is not how Mongo works. so for a document:
{ name: "my name" }
The aggregation query:
{ $cond: { $eq: ["$missingField", null] } }
Will not give true as you expect as missing is not equal to null. I took the liberty to fix the syntax issues you've had, this working pipeline is the way to go:
db.collection.aggregate([
{
$project: {
extra: {
$reduce: {
input: "$extra",
initialValue: {
cheap: [],
exp: []
},
in: {
cheap: {
"$concatArrays": [
"$$value.cheap",
{
$cond: [
"$$this.plus",
[],
[
"$$this"
],
]
}
]
},
exp: {
"$concatArrays": [
"$$value.exp",
{
$cond: [
"$$this.plus",
[
"$$this"
],
[]
]
}
]
}
}
},
},
},
}
])
Mongo Playground
One thing to note is that $cond evaluates the plus field, meaning if the field does exist with a null value or a 0 value then it will consider this document matched for the cheap array. This is something to consider and change in case these are possible.

Is it possible to query JSON data from amazon Dynamodb using javascript SDK

I need help on amazon Dynamo. I am looking to special query in dynamodb
my JSON looks below
{
blocknumber: '20',
BusinessData: {
BASE_UNIT: 'kg',
FARMERID: 'FAINKABR0001',
FARMLOCATION: 'Farm 3927',
GAPINFO: {},
PLANTINGDATE: '2020-11-02T18:30:00.000Z',
PRODUCEQUANTITES: [
{
name: 'Priya',
qty: 200
}
],
SELECTED_UNIT: {
NAME: 'kg'
}
},
chaincodeID: 'producechaincode',
docType: 'Produce',
PRID: 'PRFAINKABR0007',
PRODUCE: 'Sweetcorn',
STATUS: 'Approved',
timestamp: '2020-12-06T13:03:08.857Z'
}
I would like to query all Data(Produce) where FARMERID is 'FAINKABR0001'.
I went through all of the examples but it seems that I can query only on hash key, sort key and using GSI.
Can we query it using Javascript SDK of AWS?
Thanks in advance
The Query operation in DynamoDB finds items based on primary key values. You can query any table or secondary index (GSI) that has a composite primary key (a partition key and a sort key).
Now for your question, you have two options:
Option 1
Make FARMERID as your GSI
Option 2
Use Scan method and filter the result
Now you will need to do cost evaluation based on your need. Each method has it's own pros and cons.
PFB some references:
Scan-JS SDK
Query-DDB
Based on comment, one approach could be
var data =
[
{
blocknumber: '20',
BusinessData: {
BASE_UNIT: 'kg',
FARMERID: 'FAINKABR0001',
FARMLOCATION: 'Farm 3927',
GAPINFO: {},
PLANTINGDATE: '2020-11-02T18:30:00.000Z',
PRODUCEQUANTITES: [
{
name: 'Priya',
qty: 200
}
],
SELECTED_UNIT: {
NAME: 'kg'
}
},
chaincodeID: 'producechaincode',
docType: 'Produce',
PRID: 'PRFAINKABR0007',
PRODUCE: 'Sweetcorn',
STATUS: 'Approved',
timestamp: '2020-12-06T13:03:08.857Z'
},
{
blocknumber: '20',
BusinessData: {
BASE_UNIT: 'kg',
FARMERID: 'FAINKABR0002',
FARMLOCATION: 'Farm 3927',
GAPINFO: {},
PLANTINGDATE: '2020-11-02T18:30:00.000Z',
PRODUCEQUANTITES: [
{
name: 'Priya',
qty: 200
}
],
SELECTED_UNIT: {
NAME: 'kg'
}
},
chaincodeID: 'producechaincode',
docType: 'Produce',
PRID: 'PRFAINKABR0007',
PRODUCE: 'Sweetcorn',
STATUS: 'Approved',
timestamp: '2020-12-06T13:03:08.857Z'
},
{
blocknumber: '20',
BusinessData: {
BASE_UNIT: 'kg',
FARMERID: 'FAINKABR0001',
FARMLOCATION: 'Farm 3927',
GAPINFO: {},
PLANTINGDATE: '2020-11-02T18:30:00.000Z',
PRODUCEQUANTITES: [
{
name: 'Priya',
qty: 200
}
],
SELECTED_UNIT: {
NAME: 'kg'
}
},
chaincodeID: 'producechaincode',
docType: 'Produce',
PRID: 'PRFAINKABR0007',
PRODUCE: 'Sweetcorn',
STATUS: 'Approved',
timestamp: '2020-12-06T13:03:08.857Z'
}
];
function filterResponse(data, id) {
for(var i = 0; i < data.length; i++) {
if(data[i].BusinessData.FARMERID === id ) {
console.log(data[i]);
}
}
}
filterResponse(data, "FAINKABR0001");
Thanks #Atul Kumar for Help I have also added my whole code my be in future somebody face same issue
Here FilterExpression as FilterExpression: "BusinessData.FARMERID = :farmeridvalue"
Here we need to give FilterExpression value which attribute we want to query i.e BusinessData.FARMERID and give one name as I give farmeridvalue now you have set ExpressionAttributeValues as search value for me as FAINKABR0001
see whole scan code as below
var params = {
TableName: "Your_tableName",
FilterExpression: "BusinessData.FARMERID = :farmeridvalue",
ExpressionAttributeValues: {
":farmeridvalue" :"FAINKABR0001"
}
};
docClient.scan(params, onScan);
function onScan(err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else {
// print all the movies
console.log("Scan succeeded.", data);
data.Items.forEach(function(Block) {
console.log( "result",
Block.docType + ": ",
Block.timestamp, "- rating:", Block.BusinessData.FARMERID);
});
// continue scanning if we have more movies, because
// scan can retrieve a maximum of 1MB of data
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Scanning for more...");
params.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(params, onScan);
}
}
}

Remove object fields by updating the complete object

This is some data which is stored in a mongoDB document:
{
_id: "123"
order: 1
parent: "Dueqmd64nTxM3u9Cm"
type: "article"
unit: "kg"
}
While all the saved data will be calculated and validated first (that's why I don't use only data = { order: 2 }), I'm using the complete object to update the document:
var data = {
order: 2
parent: "Dueqmd64nTxM3u9Cm"
type: "article"
unit: "kg"
}
Collection.update(
{ _id: 123 },
{ $set: data }
);
This is working.
But if I want to remove some values, it doesn't work:
var data = {
order: 2
parent: "Dueqmd64nTxM3u9Cm"
type: "article"
unit: undefined
}
Collection.update(
{ _id: 123 },
{ $set: data }
);
I'm expecting the unit-field to get removed. But that isn't the case...
To remove fields, use the $unset operator:
data = {
unit: ""
};
Collection.update(
{ _id: 123 },
{ $set: data }
);

MongoDb's $ (update) does not update array's element but rather replace it?

I want to update an element of an array inside mongodb's document (I am using mongoose). Schema is something like:
{
..
arr : [{
foo: Number,
bar: [String],
name: String
}]
..
}
And my query is:
SomeModel.update({
_id: "id of the document",
arr: {
$elemMatch: {
_id: "_id assigned by mongoose to array element"
}
}
}, {
'arr.$': {
name: 'new name'
}
}).exec()
It just replaces whole array element say:
{
_id: "some objectId",
name: 'old name',
foo: 0,
}
to:
{
name: 'new name'
}
what I want:
{
_id: "some objectId",
name: 'new name',
foo: 0,
}
What I am curious to know if it is possible to achieve this in single update query ? (May be there is a silly mistake in my query :P or another approach)
I would also like to do update query like so:
{
$inc: { foo: 1},
$push: { bar: "abc"}
}
If you are still struggling with the whole implementation the full application of your statement is as follows:
SomeModel.update(
{
"arr._id": "123"
},
{
"$set": { "arr.$.name": "new name" },
"$inc": { "arr.$.foo": 1},
"$push": { "arr.$.bar": "abc" }
}
)
,function(err,numAffected) {
});
So each operation is performed in turn.

Categories