Mongodb sort by in array - javascript

Is there a way to sort by a given array?
something like this:
const somes = await SomeModel.find({}).sort({'_id': {'$in': [ObjectId('sdasdsd), ObjectId('sdasdsd), ObjectId('sdasdsd)]}}).exec()
What i looking for is a way to get a solution, to get all document of the collection and sort by if the document's _id match with one of the given array.
An example:
we have albums collection and songs collection. In albums collection we store the ids of the songs that belongs to the albums.
I want to get the songs, but if the song is in the album take them front of the array.
I solved this as follow, but its looks a bit hacky:
const songs = await SongMode.find({}).skipe(limit * page).limit(limit).exec();
const album = await AlbumModel.findById(id).exec();
if(album) {
songArr = album.songs.slice(limit * page);
for(let song of album.songs) {
songs.unshift(song);
songs.pop();
}
}

This cannot be accomplished using an ordinary .find().sort(). Instead, you will need to use the MongoDB aggregation pipeline (.aggregate()). Specifically, you will need to do the following:
Perform a $projection such that if the _id is $in the array, your new sort_field is given the value 1, otherwise it's given a value of 0.
Perform a $sort such that you're doing a descending sort on the new sort_field.
If you're using MongoDB version 3.4 or greater, then this is easy because of the $addFields operator:
const your_array_of_ids = [
ObjectId('objectid1'),
ObjectId('objectid2'),
ObjectId('objectid3')
];
SomeModel.aggregate([
{ '$addFields': {
'sort_field': { '$cond': {
'if': { '$in': [ '$_id', your_array_of_ids ] },
'then': 1,
'else': 0
}}
}},
{ '$sort': {
'sort_field': -1
}}
]);
If you're using an older version of MongoDB, then the solution is similar, but instead of $addFields you will be using $project. Additionally, you will need to explicitly include all of the other fields you want included, otherwise they will be excluded from the results.

Related

mongoose mongodb - remove all where condition is true except one

If a collection have a list of dogs, and there is duplicate entries on some races. How do i remove all, but a single specific/non specific one, from just one query?
I guess it would be possible to get all from a Model.find(), loop through every index except the first one and call Model.remove(), but I would rather have the database handle the logic through the query. How would this be possible?
pseudocode example of what i want:
Model.remove({race:"pitbull"}).where(notFirstOne);
To remove all but one, you need a way to get all the filtered documents, group them by the identifier, create a list of ids for the group and remove a single id from
this list. Armed with this info, you can then run another operation to remove the documents with those ids. Essentially you will be running two queries.
The first query is an aggregate operation that aims to get the list of ids with the potentially nuking documents:
(async () => {
// Get the duplicate entries minus 1
const [doc, ...rest] = await Module.aggregate([
{ '$match': { 'race': 'pitbull'} },
{ '$group': {
'_id': '$race',
'ids': { '$push': '$_id' },
'id': { '$first': '$_id' }
} },
{ '$project': { 'idsToRemove': { '$setDifference': [ ['$id'], '$ids' ] } } }
]);
const { idsToRemove } = doc;
// Remove the duplicate documents
Module.remove({ '_id': { '$in': idsToRemove } })
})();
if purpose is to keep only one, in case of concurrent writes, may as well just write
Module.findOne({race:'pitbull'}).select('_id')
//bla
Module.remove({race:'pitbull', _id:{$ne:idReturned}})
If it is to keep the very first one, mongodb does not guarantee results will be sorted by increasing _id (natural order refers to disk)
see Does default find() implicitly sort by _id?
so instead
Module.find({race:'pitbull'}).sort({_id:1}).limit(1)

Update mongoDB updating in forEach not working

I have a Node backend server, connected to a MongoDB database. Here I have a patients collections containing patients object. I'm trying to update an attribute called position on each object.
I have started by retrieving the documents from MongoDB:
const patientsToChange = await Patient.find()
Then I'm trying to update some attributes in the array by iteration over the array.
patientsToChange.forEach(function (patient) {
patient.queuePosition = parseInt(patient.queuePosition) + 1
console.log(patient._id)
let updatedPatient = patient.update({ _id: patient._id }, patient)
})
What am I missing here?
Is it even possible to update in a forEach loop?
instead of patient.update you should use Patient.update. Also it is better to use $inc for incrementing one field:
Patient.updateOne({ _id: patient._id }, { $inc: { queuePosition }} )

Firestore: Query by item in array of document

I have 2 collections "photos" and "users" and each document in "users" has one or more photo IDs with an array.
photos > 5528c46b > name: "Photo1"
a1e820eb > name: "Photo2"
32d410a7 > name: "Photo3"
users > acd02b1d > name: "John", photos: ["5528c46b"]
67f60ad3 > name: "Tom", photos: ["5528c46b", "32d410a7"]
7332ec75 > name: "Sara", photos: ["a1e820eb"]
9f4edcc1 > name: "Anna", photos: ["32d410a7"]
I want to get all users who have one or more specific photo IDs.
Are there any ways to do that?
See Henry's answer, as we've no made Array Contains queries available.
Unfortunately not yet, although it's on our roadmap.
In the meantime, you'll need to use a map instead, in the form of:
photos: {
id1: true
id2: true
}
Now you can find all users with id1 by filtering by photos.id1 == true.
Read more about querying such sets in the Firebase documentation.
Added 'array-contains' query operator for use with .where() to find documents where an array field contains a specific element.
https://firebase.google.com/support/release-notes/js 5.3.0
Update: also available in #google-cloud/firestore: https://github.com/googleapis/nodejs-firestore/releases/tag/v0.16.0
Update 2 https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html
Update 3 now available in Admin Node.js SDK v6.0.0 https://github.com/firebase/firebase-admin-node/releases
Here is a bit of expansion on the answer as some seem to be confused about having to make indexes for each key, Firestore already indexes your data for simple queries thus you can do a simple query like
documentReference.where('param','==','value').onSnapshot(...)
but you can not do a compound query unless you index your data for those parameters. So you would need indexes to be able to do something like this:
documentReference.where('param','==','value').where(..otherparams...).onSnapshot(...)
So as long as you need the photos for an id you can save them as
usersCollection : (a collection)
uidA: (a document)
photoField: (a field value that is a map or object)
fieldID1 : true (a property of the photoField)
fieldID2 : true (a property of the photoField)
etc ...
and you can simply query user(s) that have, let's say, fieldID1 in their photoField without needing to form any index and like query below.
firestore.doc('usersCollection/uidA').where('photoField.fieldID1','==',true).onSnapshot(...)
Firestore has now added an 'in' query as of November 2019.
According to the announcement article:
With the in query, you can query a specific field for multiple values
(up to 10) in a single query. You do this by passing a list containing
all the values you want to search for, and Cloud Firestore will match
any document whose field equals one of those values.
With Firebase Version 9 (Dec, 2021 Update):
You can use "array-contains" with one single photo document ID in the "while()" to get all users who have it:
import {
query,
collection,
where,
getDocs
} from "firebase/firestore";
// Here
const q = query(
collection(db, "users"),
where("photos", "array-contains", "5528c46b")
);
// Here
const usersDocsSnap = await getDocs(q);
usersDocsSnap .forEach((doc) => {
console.log(doc.data()); // "John's doc", "Tom's doc"
});
You can alse use "array-contains-any" with one or more photo document IDs with an array in the "while()" to get more corresponding users:
import {
query,
collection,
where,
getDocs
} from "firebase/firestore";
// Here
const q = query(
collection(db, "users"),
where("photos", "array-contains-any", ["5528c46b", "a1e820eb"])
);
// Here
const usersDocsSnap = await getDocs(q);
usersDocsSnap .forEach((doc) => {
console.log(doc.data()); // "John's doc", "Tom's doc", "Sara's doc"
});

Mongoose/MongoDB: $in and .sort()

I hit an API which follows 50 members' data in a game once a day, and use mongoose to convert the JSON into individual documents in a collection. Between days there is data which is consistent, for example each member's tag (an id for the member in game), but there is data which is different (different scores etc.). Each document has a createdAt property.
I would like to find the most recent document for each member, and thus have an array with each member's tag.
I an currently using the following query to find all documents where tags match, however they are returning all documents, not just one. How do I sort/limit the documents to the most recent one, whilst keep it as one query (or is there a more "mongodb way")?
memberTags = [1,2,3,4,5];
ClanMember.find({
'tag': {
$in: memberTags
}
}).lean().exec(function(err, members) {
res.json(members);
});
Thanks
You can query via the aggregation framework. Your query would involve a pipeline that has stages that process the input documents to give you the desired result. In your case, the pipeline would have a $match phase which acts as a query for the initial filter. $match uses standard MongoDB queries thus you can still query using $in.
The next step would be to sort those filtered documents by the createdAt field. This is done using the $sort operator.
The preceding pipeline stage involves aggregating the ordered documents to return the top document for each group. The $group operator together with the $first accumulator are the operators which make this possible.
Putting this altogether you can run the following aggregate operation to get your desired result:
memberTags = [1,2,3,4,5];
ClanMember.aggregate([
{ "$match": { "tag": { "$in": memberTags } } },
{ "$sort": { "tag": 1, "createdAt: -1 " } },
{
"$group": {
"_id": "$tag",
"createdAt": { "$first": "$createdAt" } /*,
include other necessary fields as appropriate
using the $first operator e.g.
"otherField1": { "$first": "$otherField1" },
"otherField2": { "$first": "$otherField2" },
...
*/
}
}
]).exec(function(err, members) {
res.json(members);
});
Or tweak your current query using find() so that you can sort on two fields, i.e. the tag (ascending) and createdAt (descending) attributes. You can then select the top 5 documents using limit, something like the following:
memberTags = [1,2,3,4,5];
ClanMember.find(
{ 'tag': { $in: memberTags } }, // query
{}, // projection
{ // options
sort: { 'createdAt': -1, 'tag': 1 },
limit: memberTags.length,
skip: 0
}
).lean().exec(function(err, members) {
res.json(members);
});
or
memberTags = [1,2,3,4,5];
ClanMember.find({
'tag': {
$in: memberTags
}
}).sort('-createdAt tag')
.limit(memberTags.length)
.lean()
.exec(function(err, members) {
res.json(members);
});
Ok, so, first, let's use findOne() so you get only one document out of the request
Then to sort by the newest document, you can use .sort({elementYouWantToSort: -1}) (-1 meaning you want to sort from newest to oldest, and 1 from the oldest to the newest)
I would recommend to use this function on the _id, which already includes creation date of the document
Which gives us the following request :
ClanMember.findOne({
'tag': {
$in: memberTags
}
}).sort({_id: -1}).lean().exec(function(err, members) {
res.json(members);
});

Reorder array in MongoDB

I'm new to MongoDB, and trying to reorder an array in a db.
Here's the schema:
headline: String,
Galleryslides: [{type: ObjectId, ref: 'Galleryslide'}],
Here's the logic I'm using. By the way, correctOrder is an array with the new order of ids for the DB.
Gallery.findById(req.params.galleryId, function(err, gallery) {
var newArr = [];
req.body.ids.forEach(function(id, index) {
newArr[index] = Galleryslides.find({"_id" : id});
});
gallery.Galleryslides = newArr;
gallery.save(function() {
res.json({status: 'ok'});
});
});
When this runs, nothing happens - the order of the array in the DB does not change. D'you know a better way to do this?
In mongodb, the records are sorted in natural order. You should get them in the same order you inserted but that's not guaranteed.
Like the docs say :
This ordering is an internal implementation feature, and you should
not rely on any particular structure within it.
If you want to sort by the _id field, you can do that(it will sort by the _id index) :
Gallery.find().sort({ "_id": 1 })

Categories

Resources