I got this user schema
const UserSchema = new Schema({
email: {
type: String,
required: true,
unique: true,
},
groups: [
{
groupName: {
type: String,
required: true,
},
groupMembers: [{ type: Schema.Types.ObjectId, ref: "GroupMember" }],
},
],
});
And I want to delete a group from the ‘groups’ array based on given ‘userId’ and ‘groupId’, my attempt (with express and mongoose):
router.delete(
"/:userId/:groupId",
catchAsync(async (req, res) => {
const { userId, groupId } = req.params;
const updatedUser = await User.findByIdAndUpdate(
userId,
{ $pull: { "groups.$._id": groupId } },
{ new: true }
);
res.send({ updatedUser });
})
);
The response of the request: I get an error: “The positional operator did not find the match needed from the query.”
Edit:
After I delete a group I need to remove all the group members in the groupMembers array.
User collection structure example:
{
"_id" : "111",
"email" : "michael#gmail.com",
"username" : "michael098",
"groups" : [
{
"_id" : "222"
"groupName" : "family",
"groupMembers" : [
{
"_id" : "333"
"name" : "Liam"
},
{
"_id" : "444"
"name" : "Noah"
}
]
},
{
"_id" : "555"
"groupName" : "friends",
"groupMembers" : [
{
"_id" : "666"
"name" : "Oliver"
}
]
}
]
}
Inside every group there is group members and I have a collection for the group members that I ref in the UserSchema : groupMembers: [{ type: Schema.Types.ObjectId, ref: "GroupMember" }]
GroupMember collection structure example:
{
{
"_id" : "333"
"name" : "Liam"
},
{
"_id" : "444"
"name" : "Noah"
},
{
"_id" : "666"
"name" : "Oliver"
}
}
For example when I get the params of userId="111" and groupId="222" I will delete the whole 'family' group and the whole group members in the groupMembers array (Liam and Noah) from the GroupMember collection.
GroupMember collection after deleting the group with _id="222":
{
{
"_id" : "666"
"name" : "Oliver"
}
}
Assuming an actual doc might look like this (using strings instead of ObjectId to keep the example tighter):
{_id:1, groups: [ { type: "ID1", ref: "XX" }, { type: "ID2", ref: "G1" }, { type: \
"ID3", ref: "G2" } ] }
then this update will remove the subdoc in the groups array where _id = 1 and type = ID2:
db.foo.update({_id:1},
{ $pull: { groups: { type: "ID2" } }}
);
Related
my Test Schema:
var TestSchema = new Schema({
testName: String,
topic: {
topicTitle: String,
topicQuestion: [
{
questionTitle: String,
choice: [
{
name: String
age: Number
}
]
}
]
}
}, { collection: 'test' });
var Test = mongoose.model('test', TestSchema);
I want to update one age ($inc)value which I have the choice id.
I can have test id, topicQuestion id and choice id.
How to write this query in mongoose in NodeJS?
Normally I use the below query to update a value:
Test.findOneAndUpdate({ _id: testId }, { $inc: { ... } }, function (err, response) {
...
});
but it is so difficult to get in array and one more array. Thanks
You can use the $[] positional operator to update nested arrays.
router.put("/tests/:testId/:topicQuestionId/:choiceId", async (req, res) => {
const { testId, topicQuestionId, choiceId } = req.params;
const result = await Test.findByIdAndUpdate(
testId,
{
$inc: {
"topic.topicQuestion.$[i].choice.$[j].age": 1
}
},
{
arrayFilters: [{ "i._id": topicQuestionId }, { "j._id": choiceId }],
new: true
}
);
res.send(result);
});
Let's say we have this existing document:
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf2116"),
"testName" : "Test 1",
"topic" : {
"topicTitle" : "Title",
"topicQuestion" : [
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf211a"),
"questionTitle" : "Question 1 Title",
"choice" : [
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf211c"),
"name" : "A",
"age" : 1
},
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf211b"),
"name" : "B",
"age" : 2
}
]
},
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf2117"),
"questionTitle" : "Question 2 Title",
"choice" : [
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf2119"),
"name" : "C",
"age" : 3
},
{
"_id" : ObjectId("5e53e7d9bf65ac4f5cbf2118"),
"name" : "D",
"age" : 4
}
]
}
]
},
"__v" : 0
}
If we want to increment age value of a given choice, we send a PUT request using endpoint like this http://.../tests/5e53e7d9bf65ac4f5cbf2116/5e53e7d9bf65ac4f5cbf211a/5e53e7d9bf65ac4f5cbf211b where
"testId": "5e53e7d9bf65ac4f5cbf2116"
"topicQuestionId": "5e53e7d9bf65ac4f5cbf211a"
"choiceId": "5e53e7d9bf65ac4f5cbf211b"
You need to inform what choice you want and, on the update section, you need change the way you do increment.
Example:
Test.findOneAndUpdate({ _id: testId, topicQuestion.choice._id: choiceId}, { 'topicQuestion.$.choice': {$inc: { age: <numberToIncrement> }}}, {new: true}, function (err, response) {
...
});
So I have this schema for a Supplier:
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Address = require('./Address.js'),
AddressSchema = mongoose.model('Address').schema,
Product = require('./Product.js'),
ProductSchema = mongoose.model('Product').schema;
// Create a new schema for the reviews collection with all the relevant information for a review
var Schema = mongoose.Schema;
var Supplier = new Schema(
{
name: String,
address: AddressSchema,
location: {
type: {type:String, default: 'Point'},
coordinates: [Number] // [<longitude>, <latitude>]
},
products: [ProductSchema]
}
);
Supplier.index({location: '2dsphere'});
var SupplierModel = mongoose.model('Supplier', Supplier );
// export the review model
module.exports = SupplierModel;
Products in my system have a "verified" field which is a boolean. In one of my routes I would like to query the DB to find all the suppliers which have products which aren't verified such that I can then render those products in the page.
I tried this, but unofrtunatelly it returns all the subdocuments no matter if "verified" is true or false:
exports.admin_accept_product_get = function (req, res) {
Supplier.find({'products.verified' : false}, function(err, docs) {
res.render('admin_accept_product', { user : req.user, suppliers: docs });
});
};
Any help is appreciated
Edit:
The previous query would return the following data:
{
"_id" : ObjectId("5b2b839a2cf8820e304d7413"),
"location" : {
"type" : "Point",
"coordinates" : [
-16.5122377,
28.4028329
]
},
"name" : "Tienda1",
"products" : [
{
"verified" : true,
"_id" : ObjectId("5b2b83d32cf8820e304d7420"),
"title" : "Vodka",
"inStock" : 15,
"typeOfItem" : "alcohol",
"sellingPrice" : 15,
"image" : "public/upload/15295784515201529168557789bottle.png",
"typeOfAlcohol" : "vodka"
},
{
"verified" : false,
"_id" : ObjectId("5b2b848f8c59960c44df09cd"),
"title" : "Whisky",
"inStock" : 40,
"typeOfItem" : "alcohol",
"sellingPrice" : 15,
"image" : "public/upload/15295786395491529323314298whisky.png",
"typeOfAlcohol" : "whisky"
}
],
"__v" : 2
}
I would like my query to not return the firt product because "verified == true"
You need to use $elemMatch to find the document and $elemMatch for projection of the data
db.collection.find({
products: {
$elemMatch: {
verified: false
}
}
},
{
products: {
$elemMatch: {
verified: false
}
},
location: 1
})
Output
[
{
"_id": ObjectId("5b2b839a2cf8820e304d7413"),
"products": [
{
"_id": ObjectId("5b2b848f8c59960c44df09cd"),
"image": "public/upload/15295786395491529323314298whisky.png",
"inStock": 40,
"sellingPrice": 15,
"title": "Whisky",
"typeOfAlcohol": "whisky",
"typeOfItem": "alcohol",
"verified": false
}
]
}
]
Check it here
I have following Mongoose schemas :
EmployeeSchema :
var EmployeeSchema = new Schema({
name : String,
employeeDetailsId: {
type: Schema.Types.ObjectId,
ref: 'employeedetails'
}
});
EmployeeDetailSchema :
var EmployeeDetailSchema = new Schema({
employeeId: {
type: Schema.Types.ObjectId,
ref: 'employee'
},
statusId: {
type: Schema.Types.ObjectId,
ref: 'status'
},
primarySkills: [
{
type: Schema.Types.ObjectId,
ref: 'skills'
}]
});
SkillsSchema :
var SkillsSchema = new Schema({
name: {
type: String,
required: true
}
});
Following is the data that's got saved in EmployeeDetails collection :
/* 1 */
{
"_id" : ObjectId("583fbbfe78854dd424f0523f"),
"employeeId" : ObjectId("583f114e1cff44b7ab414dc1"),
"statusId" : ObjectId("583ee05a1d5161941632091a"),
"secondarySkills" : [],
"primarySkills" : [],
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("583ff108cfa71d942269b09b"),
"employeeId" : ObjectId("583f114e1cff44b7ab414dc4"),
"statusId" : ObjectId("583ee05a1d5161941632091a"),
"secondarySkills" : [],
"primarySkills" : [],
"__v" : 0
}
/* 3 */
{
"_id" : ObjectId("5848c40599fa37d40a7e7392"),
"employeeId" : ObjectId("583f114e1cff44b7ab414dc8"),
"secondarySkills" : [
ObjectId("5838373072d7bab017488ba2")
],
"primarySkills" : [
ObjectId("5848c3c299fa37d40a7e7390"),
ObjectId("5848c3d599fa37d40a7e7391")
],
"__v" : 0
}
/* 4 */
{
"_id" : ObjectId("5848c41699fa37d40a7e7393"),
"employeeId" : ObjectId("583f114e1cff44b7ab414dc6"),
"secondarySkills" : [],
"primarySkills" : [
ObjectId("5838373072d7bab017488ba2"),
ObjectId("5848c3c299fa37d40a7e7390"),
ObjectId("5848c3d599fa37d40a7e7391")
],
"__v" : 0
}
UseCase :
When i want to group EmployeeDetails collection based on Status ID, i used following aggregation in Mongoose :
EmployeeDetailsModel.aggregate([
{
$group: {_id: "$statusId", count: {$sum: 1}}
}
]).exec(...);
In similar way, i want to group based on primarySkills or secondarySkills where both of them are array of Skill ObjectID's.
I tried few approaches but no luck. Need some help.
So if you are trying to get a result that shows a list of employees who has a certain skill, $unwind might help.
db.emp.aggregate([{$unwind:"$primarySkills"},{$group:{"_id":"$primarySkills", "employees":{$push:"$employeeId"}}}])
And here's the result:
{ "_id" : ObjectId("5848c3d599fa37d40a7e7391"), "employees" : [ ObjectId("583f114e1cff44b7ab414dc6"), ObjectId("583f114e1cff44b7ab414dc8") ] }
{ "_id" : ObjectId("5848c3c299fa37d40a7e7390"), "employees" : [ ObjectId("583f114e1cff44b7ab414dc6"), ObjectId("583f114e1cff44b7ab414dc8") ] }
{ "_id" : ObjectId("5838373072d7bab017488ba2"), "employees" : [ ObjectId("583f114e1cff44b7ab414dc6") ] }
The $unwind doc.
I want to create a subdocument in a subobject field, not to update.
My Schema:
var DemandeSchema = new Schema({
titre: {
type: String,
required: true
},
description: {
type: String,
required: true
},
type: {
type: String,
required: true
},
answer: {}
});
My code:
demande.update(
{ name: 'answer' },
{ $push: req.body.answer },
{ upsert: true },
function(error, user) {
if (error) return next(error);
else {
return true;
}
}
)
req.body.answer = {
"id": "57f512f4360d8818a4e5ea3d",
"answer": {
"122547eee99" : {
"review" : "1.3",
"login" : "new"
}
}
}
But this code doesn't create a new field in my DB, it just updates the field answer when I just want to create a new object field in the answer field.
Actual Result:
{
"_id" : ObjectId("57f512f4360d8818a4e5ea3d"),
"titre" : "TEST",
"description" : "ee",
"type" : "ee",
"__v" : 0,
"answer" : {
"122547eee98" : {
"review" : "8.8",
"login" : "x"
}
}
}
Expected Result:
{
"_id" : ObjectId("57f512f4360d8818a4e5ea3d"),
"titre" : "TEST",
"description" : "ee",
"type" : "ee",
"__v" : 0,
"answer" : {
"122547eee98" : {
"review" : "8.8",
"login" : "x"
},
"122547eee99" : {
"review" : "1.3",
"login" : "new"
}
}
}
var DemandeSchema = new Schema({
titre: {
type: String,
required: true
},
description: {
type: String,
required: true
},
type: {
type: String,
required: true
},
answer: []
});
Answer field curly braces would convert to square brackets for pushing all new answers.
Conclusion: It creates an array.
Instead of the $push operator which works on arrays, use the $set operator together with the dot notation to set the subdocument in the embedded answer document.
You would need to preprocess the document to use in your update so that it will have the dot notation. The following mongo shell example demonstrates this:
var obj = {
"id": "57f512f4360d8818a4e5ea3d",
"answer": {
"122547eee99" : {
"review" : "1.3",
"login" : "new"
}
}
},
update = {};
var key = Object.keys(obj.answer)[0]; // get the dynamic key "122547eee99"
update["answer."+key] = obj.answer[key]; // create the update object with dot notation
/*
update = {
"answer.122547eee99": {
"review" : "1.3",
"login" : "new"
}
}
*/
db.demandes.update(
{ "_id" : ObjectId(obj.id)},
{ "$set": update },
{ "upsert": true }
)
Using the same concept as above, you can create the documents to use in your update as follows:
var update = {},
key = Object.keys(req.body.answer.answer)[0]; // get the dynamic key "122547eee99"
// create the update object with dot notation
update["answer."+key] = req.body.answer.answer[key];
demande.update(
{ "_id": req.body.answer.id },
{ $set: update },
{ upsert: true },
function(error, user) {
if (error) return next(error);
else {
return true;
}
}
);
Try this, and in schema answer: [],
demande.findOne( { name: 'answer' }, function(err, result){
result.answer.push({ans:req.body.answer})
var dem = new Demande(result); // Demande is ur mongoose schema model,
dem.save(function(err, result){
console.log(result);
});
})
I have ItemCatalog collection which contains type ,unit, items.
var Categories = new Schema({
typeName: String
});
var MeasurementUnit = new Schema({
unit: String
});
var Items = new Schema({
itemName: String,
itemStrength: String,
idType: { type: Schema.Types.ObjectId, ref: 'Categories' },
idUnit: { type: Schema.Types.ObjectId, ref: 'MeasurementUnit' },
isActive: Boolean
});
var ItemCatalog = new Schema({
type: Categories,
unit: MeasurementUnit,
items: Items
});
document of collection looking like below
{
"_id": ObjectId("57b188d67aa27ae4ee87e11c"),
"type": [
{
"_id" : ObjectId("57b188d6e128064381ae2f2f"),
"typeName" : "abc"
}
],
"__v": 0,
"unit": [
{
"_id" : ObjectId("57b188e4e128064381ae2f54"),
"unit" : "mg"
}
],
"items": [
{
"itemStrength" : "100",
"itemName" : "a1",
"idType" : ObjectId("57b188d6e128064381ae2f2f"),
"idUnit" : ObjectId("57b188e4e128064381ae2f54"),
"isActive" : true,
"_id" : ObjectId("57b188f3e128064381ae2f7a")
}
]
}
How can I retrieve items data which are active and populating idType and idUnit instead of fetching entire document and looping at the client?
Want data something like this
{
_id: "57b188d67aa27ae4ee87e11c",
drugs: [
{
itemStrength: "100",
itemName: "a1",
typeName: "abc",
unit: "mg",
isActive: true,
_id: "57b188f3e128064381ae2f7a"
}
]
}
Please suggest best ways implement same.
Try this:
ItemCatalog.find({"items.isActive" : true},{'items.$' : 1}).populate('items.$.idType').populate('items.$.idUnit').exec(function(err,result){...});
items.isActive to required check which is active.
items.$ is provided to return only those elements of items array which are active.
item.$.idType is required to populate only from matched array element.
Hope this works for you.