How do I query an array within a schema in mongoose? - node.js

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 }
)

Related

Getting an {"message":"Invalid update pipeline operator: \"_id\""} error

I am trying to update two rows in my players table based on the id. I am trying to use the updateMany method where id can be found in an array of id's but I am getting the {"message":"Invalid update pipeline operator: \"_id\""} error. I checked the array to make sure it is valid id's. Here is my code
const winningTeam = asyncHandler(async (req, res) => {
req.body.forEach((element) => {
element.wins += 1;
element.lastPlayed = Date.now();
element.percentage = (element.wins / (element.wins + element.wins)) * 1000;
});
let usersId = [];
usersId.push(req.body[0]._id);
if (req.body.length === 2) {
usersId.push(req.body[1]._id);
}
const player = await Player.updateMany({ _id: { $in: usersId } }, req.body);
if (player) {
res.status(200).json(player);
} else {
res.status(400);
throw new Error("Invalid Data");
}
});
You should use $set property for the update parameter. I'm not sure about the structure of your req.body but it should be something like this:
Player.updateMany({ _id: { $in: usersId } }, {$set: req.body});
instead of this:
Player.updateMany({ _id: { $in: usersId } }, req.body);
Take a look at docs for updateMany

Delay in return value - nodejs & mongoose

I'm fairly new to nodejs and I'm doing a full stack developer challenge from devchallenges.io (Shoppingify). Below, I'm trying to increase the quantity value based on whether the user clicks to increase or decrease the item quantity. However, there's a slight delay between the return value from the request and the actual value in the database. It seems that the value updates immediately which is great however, the return value in the request is the previous value rather than being the current quantity value in the database.
mongoDB Database
// #route PUT api/list/item/quantity/:id
// #desc update item quantity
// #access Private
router.put('/item/quantity/:id', auth, async (req, res) => {
const { action } = req.body;
try {
let list = await List.findOne({ user: req.user.id });
// find current quantity
const item = list.items.find((item) => {
return item._id.toString() === req.params.id;
});
// increase quantity
if (action === 'increase') {
list = await List.findOneAndUpdate(
{ 'items._id': req.params.id },
{ $set: { 'items.$.quantity': item.quantity + 1 } },
{ new: true }
);
} else {
// decrease quantity
list = await List.findOneAndUpdate(
{ 'items._id': req.params.id },
{ $set: { 'items.$.quantity': item.quantity - 1 } },
{ new: true }
);
}
res.json(item.quantity);
} catch (error) {
console.error(error.message);
res.status(500).send('Server Error');
}
});
You are defining item in here:
const item = list.items.find((item) => {
return item._id.toString() === req.params.id;
});
At this point list is the "old" version of the object, you want to be doing the same after the update when the list object is updated and only then to return it.
// this is the original "list" item
let item = list.items.find((item) => {
return item._id.toString() === req.params.id;
});
...
update list
...
// now "list" is updated
item = list.items.find((item) => {
return item._id.toString() === req.params.id;
});
I will just add two additional tips to improve performance, they are mutually exclusive so you'll have to choose one of the two.
in the update query add the list._id to it, If I were to guess the collection does not have an index on the items field ( and if it does it's a bad idea usually ). this means when you updated just using the item._id field it takes longer for mongo to find the object. it's quick change to both updates:
list = await List.findOneAndUpdate(
{ _id: list._id, 'items._id': req.params.id },
{ $set: { 'items.$.quantity': item.quantity - 1 } },
{ new: true }
);
(my preferred option) do it in a single call, using the update arrayFilters option, like so:
const list = await List.findOneAndUpdate(
{
user: req.user.id,
},
{
$inc: {
'items.$[elem].quantity': action === 'increase' ? 1 : -1,
},
},
{
arrayFilters: [
{
'elem._id': new ObjectId(req.params.id),
},
],
new: true,
});
const item = list.items.find((item) => {
return item._id.toString() === req.params.id;
});
Mongo Playground
In my opinion now your route looks much better, you're also cutting down from 2 db calls to 1.

Sorting in mongoose

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.

Mongoose is only returning ID from MongoDB

I am currently trying to incorporate datatables with my MongoDB database. I am having some trouble accessing the returned object though. The main problem I am seeing is that I am only getting the _id returned from MongoDB, and no values of the object.
Heres the code I am using to pass the information to the datatables.
var itemsModel = require('./models/itemReturn');
exports.getItemList = function(req, res) {
var searchStr = req.body.search.value;
if (req.body.search.value) {
var regex = new RegExp(req.body.search.value, "i")
searchStr = { $or: [{ 'productName': regex }, { 'itemPrice': regex }, { 'Quantity': regex }, { 'Description': regex }, { 'seller': regex }] };
} else {
searchStr = {};
}
var recordsTotal = 0;
var recordsFiltered = 0;
itemsModel.count({}, function(err, c) {
recordsTotal = c;
console.log(c);
itemsModel.count(searchStr, function(err, c) {
recordsFiltered = c;
itemsModel.find(searchStr, 'productName itemPrice Quantity Description seller', { 'skip': Number(req.body.start), 'limit': Number(req.body.length) }, function(err, results) {
if (err) {
console.log('error while getting results' + err);
return;
}
var data = JSON.stringify({
"draw": req.body.draw,
"recordsFiltered": recordsFiltered,
"recordsTotal": recordsTotal,
"data": results
});
console.log(data);
res.send(data);
});
});
});
};
This is the model
// app/models/itemsReturn.js
// load the things we need
var mongoose = require('mongoose');
var schemaOptions = {
timestamps: true,
toJSON: {
virtuals: true
},
toObject: {
virtuals: true
}
};
// define the schema for our item model
var itemsReturned = mongoose.Schema({
productName: String,
itemPrice: String,
Quantity: String,
Description: String,
seller: String
}, schemaOptions);
// create the model for users and expose it to our app
var items = mongoose.model('items', itemsReturned);
module.exports = items;
The thing is that I know its not a data table issue as I can make the _id appear in the tables. I just need to know how to return the entire object instead of just the _ID so that I can access the values of the object.
If it helps this is the tutorial I am following.
UPDATE: Okay so I figured out why my MongoDB collections were only returning the item ID. The issue was that I had stored everything in the local database (oops).

Mongoose cascading deletes in same model

This is different than this and this. But they are very helpful.
Basically, I have a Topic schema. If one Topic get's deleted, I want to delete other topics. Think of a graph where deleting a node means deleting the edges.
var schema = new Schema({
title: { type: String, required: true, trim: true },
srcId: { type: Schema.Types.ObjectId, validate: [edgeValidator, 'Set both srcId and destId, or neither'] },
destId: Schema.Types.ObjectId,
});
I want the 2nd mongo delete to run in the schema.pre('remove', ...)
But I don't have a model at this point. So calling .find() or .remove() doesn't work. What's the best way?
schema.pre('remove', function(next) {
var query = '';
var self = this;
if (this.isEdge) {
query = 'MATCH ()-[r:CONNECTION { mongoId: {_id} }]-() DELETE r;';
} else {
query = 'MATCH (n:Topic { mongoId: {_id} })-[r]-() DELETE n,r;';
}
// This is another database.
neo.query(query, this, function(err, data) {
if (err) return next(err);
if (self.isEdge) {
return next();
} else {
// Now we're back to mongoose and mongodb
// Find and remove edges from mongo
schema.find({ mongoId: { // <------ .find() is undefined
$or: [
{ srcId: self._id },
{ destId: self._id }
]
}}, function(err, edges) {
edges.remove(next);
});
}
});
});
This turned out to be pretty easy.
var Model = null;
var schema = ...
module.exports = Model = mongoose.model('Topic', schema);
Then just use Model in the pre-remove. Piece of pie.

Resources