Sorting in mongoose - node.js

Alright, this is very wierd but the sort does not work. it does not throw any error but the sort does not work.
try {
properties = await Property.find({}).sort("-minimumPrice");
} catch (err) {
console.log(err)
}
I also tried but it didnt work as well:
try {
properties = await Property.find({}).sort({minimumPrice: "desc"});
} catch (err) {
console.log(err)
}

See here for some decent answers on sorting and
here is some good official docs on mongoose async/await
You should use .exec() with await for better stack traces, the sort can take these values: asc, desc, ascending, descending, 1, and -1.
try {
let properties = await Property.find(query).sort({"minimumPrice": -1}).exec()
} catch (err) {
console.log(err)
}
This is all assuming your query is correct and is retrieving documents to be sorted.
UPDATE
I went through your whole situation and created a test using what you provided.
const mongoose = require("mongoose");
var Schema = mongoose.Schema;
var propertySchema = new Schema({
name: String,
minimumPrice: Number
});
var Property = mongoose.model('Property', propertySchema);
//Testing
(async function() {
try {
//connect to mongo
await mongoose.connect('mongodb://localhost:27017/testing', { useNewUrlParser: true, useUnifiedTopology: true });
//First, delete all properties
await Property.deleteMany({}).exec();
let properties = [];
//Insert 5 properties
for (var i = 1; i < 6; i++) {
properties.push({ name: "property" + i, minimumPrice: Math.round(Math.random() * 10000) });
}
//Insert all our random properties
await Property.create(properties);
console.log(properties);
//Now, retrieve all our properties
let sortedProperties = await Property.find({}).sort({ minimumPrice: -1 }).exec();
console.log("sorted", sortedProperties);
} catch (err) {
console.log(err);
}
})();
Database Input:
[
{ name: 'property1', minimumPrice: 3846 },
{ name: 'property2', minimumPrice: 7910 },
{ name: 'property3', minimumPrice: 7234 },
{ name: 'property4', minimumPrice: 4444 },
{ name: 'property5', minimumPrice: 6366 }
]
Sorted Output:
[
{
name: 'property2',
minimumPrice: 7910
},
{
name: 'property3',
minimumPrice: 7234
},
{
name: 'property5',
minimumPrice: 6366
},
{
name: 'property4',
minimumPrice: 4444,
},
{
name: 'property1',
minimumPrice: 3846
}
]
You can see the properties come back sorted. Which leads me to assume, somewhere you've inserted your minimumPrice as a string.

Related

node mongoose how to auto increment

Trying to follow the example here:
https://www.tutorialspoint.com/mongodb/mongodb_autoincrement_sequence.htm
export interface RowProps {
id?: number; // This is to auto increment
todoText: string;
}
const addAutoIncrement = async ({ db, collectionName, todoText }) => {
const getNextSequenceValue = (sequenceName: string) => {
const sequenceDocument = db
.collection<RowProps>(collectionName)
.findAndModify({
query: { _id: sequenceName },
update: { $inc: { sequence_value: 1 } },
new: true,
});
console.log('sequenceD', sequenceDocument)
return sequenceDocument.sequence_value;
};
db.collection<RowPropsClient>(collectionName).insertOne(
{
id: getNextSequenceValue('id'),
todoText
},
(err) => {
if (err) {
console.log("err");
}
}
);
}
// db is already defined and works
// I can add to the collection so this also works.
addAutoIncrement({ db, collectionName: 'todos', todoText: 'hello' });
Error: throw new Error('Collection#findAndModify unimplemented by driver');
^
Error: Collection#findAndModify unimplemented by driver
update
Tried to follow this example:
https://medium.com/#salonimalhotra1ind/how-to-increment-a-number-value-in-mongoose-785066ba09d8
const addAutoIncrement = async ({ db, collectionName, todoText }) => {
const modelTodo = db.model(collectionName, TodosSchema);
const res = await new modelTodo({ todoText }).save();
const { _id } = res;
return new Promise((resolve, reject) => {
modelTodo.findOneAndUpdate(
{ _id },
{ $inc: { id: 1 } },
{ new: true },
(err, res) => {
if (err) {
reject(err);
}
resolve(res);
}
);
});
};
**The result is just setting the value to 1 each time - not incrementing**
Collection#findAndModify() is a method that is implemented in the MongoDB shell, but not in the Node.js driver.
You should use Collection#findOneAndUpdate instead:
const { value : sequenceDocument } = db
.collection<RowProps>(collectionName)
.findOneAndUpdate({
{ _id: sequenceName },
{ $inc: { sequence_value: 1 } },
{ returnDocument : 'after' } // equivalent to `new: true`
});
ok I don't know why I didnt do this before. All the online examples make everything unnecessarily complicated.
Just get the total count and then add it.
const addAndIncrement = async ({ db, collection, todoText }) => {
const connectedModel = db.model(collection, TodosSchema);
const documentCount = await connectedModel.count({}); // get the total count
return new connectedModel({ todoText, id: documentCount }).save();
};
Unless anyone comes up with a more performant way, this is what I'm going with.

How do I query an array within a schema in mongoose?

Really stuck on querying an array nested within a schema.
The list schema looks likes this:
const listSchema = new mongoose.Schema({
name: String,
items: [cardSchema]
});
I want to be able to query a particular list for a particular item, and figured this would work:
const listId = req.body.listId;
const itemId = req.body.itemId;
List.updateOne({
"_id": listId
}, {
$pull: {
items: {
"_id": itemId
}
}
});
As I understand, I'm updating a list by the id of "listId", and then removing something from it. The thing I'm removing is within the "items" array and has the id "itemId". However, this doesn't do anything.
I have a much clumsier, heavy-handed way of getting round this problem, shown here:
List.findOne({
_id: listId
}, function (err, list) {
if (err) {
console.log(err)
} else {
const listItems = list.items
listItems.forEach(function (item) {
if (item._id == itemId) {
const index = listItems.findIndex(function (i) {
return i.id === itemId
})
if (index !== -1) listItems.splice(index, 1);
list.save();
}
})
}
But I'd much rather something like the former. The latter just seems ridiculous when nesting arrays within arrays within arrays etc so I know this can't be the sensible way.
I found the solution eventually:
List.findOne({listId}, function (err, list) {
list.items.id(itemId).remove();
list.save();
});
Given the following Schema:
const listSchema = new mongoose.Schema({
name: String,
items: [cardSchema]
});
and the following ids:
const listId = req.body.listId;
const itemId = req.body.itemId;
If your final goal is to remove item with itemId, I believe the code below should do the trick:
List.findOneAndUpdate(
{ "_id": listId },
{ $pull: { items: { _id: itemId} } },
{ 'new': true }
);
The core idea is this:
Collection.findOneAndUpdate(
{ _id: yourCollectionId },
{ $pull: { subdocumentsArray: { _id: subdocumentId} } },
{ new: true }
)

Sequelize how to get all data if there is no query string passed

I'm pretty new to Sequelize.
I'm trying to create a handler to get all playlists in my database.
This is what I want to do:
If there is a query string then it should return the result based on that query.
If there is no query string passed then it should return all my playlists.
This is my playlist model:
const Playlist = db.define("playlist", {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: Sequelize.STRING,
unique: true,
},
});
Here is my handler:
exports.getPlaylists = async (req, res) => {
try {
const { name } = req.query;
console.log(name); // this prints the name
const result = await Playlist.findAll({
where: {
name:
name === undefined ? {} : { $like: `%${name}%` },
},
});
if (result) {
res.status(200).send(result);
} else {
response.status(404).send("No Playlists found");
}
} catch (error) {
res.status(500).send(`Internal server error: ${error}`);
}
};
This works well if I passed a name in the query. but If I didn't pass any query string. It returns an empty array.
$like is an alias for Sequelize.Op.like
What should I put instead of the empty object?
I checked this question How get all data if field in query string is empty Node and Sequelize with Postgres but the proposed solutions didn't work with me
Create a filter object based on the condition. If you pass empty object to where, it won't apply that in the query.
exports.getPlaylists = async (req, res) => {
try {
const { name } = req.query;
const filters = {};
if (name)
filters.name = {
[Op.like]: `${name}%`, // you can also use $like if you are using older version of sequelize
}
const result = await Playlist.findAll({
where: filters,
});
if (result) {
res.status(200).send(result);
} else {
response.status(404).send("No Playlists found");
}
} catch (error) {
res.status(500).send(`Internal server error: ${error}`);
}
};
This way you can prepare complex filters based on other query strings.

When I make db process inside async map function, I can't avoid duplicate

I want to add pallet barcode to palletBarcodes field of record.But there is check for avoid add same palletBarcode.I am using below function. But check is not working inside async map function.
myService.js
const palletBarcodes = ["TP2","TP2"]
await Promise.all(palletBarcodes.map(async (palletBarcode) => {
const promise = await this.addPalletBarcode({ transferId, barcode: palletBarcode });
return promise;
}));
async addPalletBarcode({ transferId, barcode, pickerId }) {
const { TransferDataAccess } = this;
const transfer = await TransferDataAccess.getTransfer({ transferId });
if (!transfer) {
throw new TransferNotFoundError();
}
if (transfer.palletBarcodes.length && transfer.palletBarcodes.includes(barcode)) {
throw new PalletBarcodeAlreadyExistsError({ barcode });
}
return TransferDataAccess.pushPalletBarcode({ transferId, barcode });
}
transferDataAccess:
async pushPalletBarcode({ transferId, barcode }) {
const { TransferModel } = this;
return TransferModel
.findOneAndUpdate({
_id: transferId,
},
{
$push: {
palletBarcodes: barcode,
},
})
.lean()
.exec();
}
Instead of $push use $addToSet. $addToSet will treat your key in document as a set and that will automatically avoid duplicates.
You query would then become -
TransferModel.findOneAndUpdate(
{ _id: transferId },
{ $addToSet: { palletBarcodes: barcode } }
);

Mongoose nested document update failing?

If I have a nested document, how can I update a field in that nested document in Mongoose?
I carefully researched this problem using everything available I could find, and even changed my test code to match a similar answered question about this here on Stackoverflow, but I am still unable to figure this out. Here are is my Schema and Models, the code, and the Mongoose debug output. I am unable to understand what I am doing wrong, here.
var mongoose = require('mongoose')
, db = mongoose.createConnection('localhost', 'test')
, assert = require("node-assert-extras");
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
db.once('open', function () {
// yay!
});
mongoose.set('debug', true);
var PDFSchema = new Schema({
title : { type: String, required: true, trim: true }
})
var docsSchema = new Schema({
PDFs : [PDFSchema]
});
var A = db.model('pdf', PDFSchema);
var B = db.model('docs', docsSchema);
function reset(cb) {
B.find().remove();
// create some data with a nested document A
var newA = new A( { title : "my title" })
var newB = new B( { PDFs: newA});
newB.save();
cb();
}
function test1( ) {
reset(function() {
B.findOne({}, 'PDFs', function(e,o)
{
console.log(o);
pdf_id = o.PDFs[0]._id;
console.log("ID " + pdf_id);
B.update(
{ 'pdfs.pdf_id': pdf_id },
{ $set: {
'pdfs.$.title': 'new title'
}}, function (err, numAffected) {
if(err) throw err;
assert.equal(numAffected,1); //KA Boom!
}
);
});
});
}
test1();
/*
$ node test2.js
Mongoose: docs.remove({}) {}
Mongoose: docs.findOne({}) { fields: { PDFs: 1 }, safe: true }
Mongoose: docs.insert({ __v: 0, PDFs: [ { _id: ObjectId("50930e3d0a39ad162b000002"), title: 'my title' } ], _id: ObjectId("50930e3d0a39ad162b000003") }) { safe: true }
{ _id: 50930e3d0a39ad162b000003,
PDFs: [ { _id: 50930e3d0a39ad162b000002, title: 'my title' } ] }
ID 50930e3d0a39ad162b000002
assert.js:102
throw new assert.AssertionError({
^
AssertionError: 0 == 1
*/
You're not using the correct field names in your B.update call. It should be this instead:
B.update(
{ 'PDFs._id': pdf_id }, // <== here
{ $set: {
'PDFs.$.title': 'new title' // <== and here
}}, function (err, numAffected) {
if(err) throw err;
assert.equal(numAffected,1);
}
);
You should also fix your reset function to not call its callback until the save completes:
function reset(cb) {
B.find().remove();
// create some data with a nested document A
var newA = new A( { title : "my title" })
var newB = new B( { PDFs: newA});
newB.save(cb); // <== call cb when the document is saved
}

Resources