Can't Store Nested JSON Array to MongoDB Using Mongoose - arrays

I have JSON on Postman like this:
{
"hostname": [
{
"item": [
{
"system": "10l313",
"severity": "2"
},
{
"system": "2131414",
"severity": "3"
}
]
},
{
"item": [
{
"system": "4234235",
"severity": "4"
}
]
}
]
}
I want to create new collections in mongodb from json above. It's just a little picture of the actual json array, the above json array can contain an enormous array. I am confused how to save as many json arrays using mongoose, do i have to loop as much as array length or is there other easier way?
mongoose schema:
var ItemSchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
system: {
type: String
},
severity: {
type: Number
}
})
var VulnSchema = new Schema({
hostname: [{
item: [{ItemSchema}]
}]
});
controller:
exports.create_vulnerabilities = function (req, res) {
var vuln = new Vuln ({
_idFIle: mongoose.Types.ObjectId(),
hostname: req.body.hostname
});
vuln.save()
.then(result => {
res.status(201).json({
result
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
};
I have tried running my code but the result is like this. The problem is system and severity attribute are not stored in mongodb.
{
"_id" : ObjectId("5b4c39a301651a0fc047bec7"),
"hostname" : [
{
"_id" : ObjectId("5b4c39a301651a0fc047beca"),
"item" : [
{
"_id" : ObjectId("5b4c39a301651a0fc047becc")
},
{
"_id" : ObjectId("5b4c39a301651a0fc047becb")
}
]
},
{
"_id" : ObjectId("5b4c39a301651a0fc047bec8"),
"item" : [
{
"_id" : ObjectId("5b4c39a301651a0fc047bec9")
}
]
}
],
"__v" : 0
}
Please help me. thank you

Change
var VulnSchema = new Schema({
hostname: [{
item: [{ItemSchema}]
}]
});
to
var VulnSchema = new Schema({
hostname: [{
item: [ItemSchema]
}]
});
Example try running this:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
const Schema = mongoose.Schema,ObjectId = Schema.ObjectId;
var ItemSchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
system: {
type: String
},
severity: {
type: Number
}
})
var VulnSchema = new Schema({
hostname: [{
item: [ItemSchema]
}]
});
const Vuln = mongoose.model('Vuln', VulnSchema);
var hostname = [
{
"item": [
{
"system": "10l313",
"severity": "2"
},
{
"system": "2131414",
"severity": "3"
}
]
},
{
"item": [
{
"system": "4234235",
"severity": "4"
}
]
}
]
var vuln = new Vuln ({
_idFIle: mongoose.Types.ObjectId(),
hostname: hostname
});
vuln.save()
.then(result => {
console.log(JSON.stringify(result))
})
.catch(err => {
console.log(err);}
);

Related

how to refactoring $expr, $regexMatch filter for easier reading React/MongoDB?

I would like to explain my problem of the day.
Currently I perform a filter on an input which allows me to search the last name and first name it works really well
I have deleted a lot of things for a simpler reading of the code if there is a need to bring other element do not hesitate to ask
const {
data: packUsersData,
} = useQuery(
[
"pack",
id,
"users",
...(currentOperatorsIds.length ? currentOperatorsIds : []),
value,
],
async () => {
const getExpr = () => ({
$expr: {
$or: [
{
$regexMatch: {
input: {
$concat: ["$firstName", " ", "$lastName"],
},
regex: value,
options: "i",
},
},
{
$regexMatch: {
input: {
$concat: ["$lastName", " ", "$firstName"],
},
regex: value,
options: "i",
},
},
],
},
});
let res = await usersApi.getrs({
pagination: false,
query: {
"roles.name": "operator",
_id: { $nin: currentOperatorsIds },
deletedAt: null,
$or: value
? [
{
entities: [],
...getExpr(),
},
{
entities: { $in: id },
...getExpr(),
},
]
: [
{
entities: [],
},
{
entities: { $in: id },
},
],
},
populate: "entity",
sort: ["lastName", "firstName"],
});
{
refetchOnMount: true,
}
);
and so i find the read a bit too long have any idea how i could shorten all this?
thx for help.
You can reduce entities field $or condition, just concat the empty array and input id,
let res = await usersApi.getrs({
pagination: false,
query: {
"roles.name": "operator",
_id: { $nin: currentOperatorsIds },
deletedAt: null,
entities: { $in: [[], ...id] },
...getExpr()
},
populate: "entity",
sort: ["lastName", "firstName"]
});
If you want to improve the regular expression condition you can try the below approach without using $expr and aggregation operators,
create a function and set input searchKeyword and searchProperties whatever you want to in array of string
function getSearchContiion(searchKeyword, searchProperties) {
let query = {};
if (searchKeyword) {
query = { "$or": [] };
const sk = searchKeyword.trim().split(" ").map(n => new RegExp(n, "i"));
searchProperties.forEach(p => {
query["$or"].push({ [p]: { "$in": [...sk] } });
});
}
return query;
}
// EX:
console.log(getSearchContiion("John Doe", ["firstName", "lastName"]));
Use the above function in query
let res = await usersApi.getrs({
pagination: false,
query: Object.assign(
{
"roles.name": "operator",
_id: { $nin: currentOperatorsIds },
deletedAt: null,
entities: { $in: [[], ...id] }
},
getSearchContiion(value, ["firstName", "lastName"])
},
populate: "entity",
sort: ["lastName", "firstName"]
});

How can I combine the results of 3 queries in MongoDB?

I made the following filter in hopes that I would be combining the results from all 3 $and arrays but it is only matching one of those blocks.
How can I combine the results of what would be returned from each $and array if conditions are met. Hopefully that's clear. I don't know what to call the $and array.
const filter = {
$or: [
{
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
name: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
description: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
tags: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
},
],
};
await Content.paginate(filter, options, (err, result) => {
if (err) {
res.status(500).send(err);
} else {
res.json(result);
}
});
EDIT: Below is an example of two entries that would be found in the database. The way it should work is it should use category, subCategory, and contentType to filter out the entries in the database so that what I have now are only the entries which have the same category, subCategory, and contentType as specified in req.query, I'll call this the firstFilterResult. From there, I am trying to search within firstFilterResult to see if I have entries that have name, tag, or description matches. So basically catgeory, subCategory and contentType are just used to narrow down the results so that I can find matches for name, tag, and description. My code above doesn't do exactly this but this is the idea behind it and I thought that what I have would do similar, but I guess I'm wrong.
contents: [
{
tags: [
'food',
'drinks',
'card',
'account'
],
_id: '1d13ff7m6db4d5417cd608f4',
name: 'THE NAME FOR THIS PIECE OF CONTENT',
description: 'In here I will begin to talk about...',
content_id: '5dbcb998ad4144390c244093',
contentType: 'quiz',
date: '2019-06-03T04:00:00.000Z',
category: 'food',
image: 'https://IMAGE.PNG',
__v: 0
},
{
tags: [
'computer',
'laptop'
],
_id: '7d1b940b1c9d44000025db8c',
name: 'THE NAME FOR THIS PIECE OF CONTENT',
description: 'This is another description',
content_id: '5f1b963d1c9d44000055db8d',
contentType: 'tool',
date: '2019-06-03T04:00:00.000Z',
category: 'money',
image: 'https://IMAGE.PNG',
__v: 0
}
]
I finally got it to work with this
const catFilter =
req.query.category !== "" ? { category: req.query.category } : {};
const subCatFilter =
req.query.subCategory !== "" ? { tags: req.query.subCategory } : {};
const typeFilter =
req.query.contentType !== ""
? { contentType: req.query.contentType }
: {};
const filter = {
$and: [
{
$or: [
{
name: {
$regex: req.query.searchTerm,
$options: "i",
},
},
{
description: {
$regex: req.query.searchTerm,
$options: "i",
},
},
{
tags: {
$regex: req.query.searchTerm,
$options: "i",
},
},
],
},
catFilter,
subCatFilter,
typeFilter,
],
};
Since each element of the $or contains the same 3 checks with a single one that varies, these can be separated out, and the $or is then only needed if a search term is specified.
Passing options:"i" makes the entire regex match case insensitive, so it is not necessary to surround the search string with (?i) and (?-i)
The following should build the filter that you are attempting, without using empty objects:
// base query that checks the common fields
var filter = {
category: req.query.category,
tags: req.query.subCategory,
contentType: req.query.contentType
};
// if a search term is provided, add in the additional critera
if (req.query.searchTerm !== "") {
var regex = {
$regex: req.query.searchTerm,
options:"i"
};
filter['$or'] = [
{ name: regex },
{ description: regex },
{ tags: regex }
]
}
If this doesn't obtain the results you're after, please edit the question and add in some sample documents so we can see the problem.

mongoose populate after another populate

I have 3 mongo collections.
resource
access
user
the user has a relationship with access and access has a relationship with resource. I want when populate a user all join be done and I see the full resource that user has access them.
I tried this code but was n't expected response
Users.findById(req.params.userId)
.populate({path:'accessID',model:'accesses'})
.populate({path:'accessID.accessLists',model:'resources'})
and this
Users.findById(req.params.userId)
.populate({path:'accessID',model:'accesses'})
.then
(
(user)=>
{
Users.populate(user,{path})
}
)
here is my defections
Resource:
var ResourceSchema =new Schema
({
resource:String
method:
{ type:String,
enum:["GET","PUT","POST","DELETE"]
}
})
Access:
var AccessSchema =new Schema
({
name:String
access_List:
[
{ type: Schema.Types.ObjectId, ref:'resources'}
]
})
User:
var UserSchema = new Schema
({
name:String,
email:String,
accessID:
{ type: Schema.Types.ObjectId, required: true, ref: 'accesses' }
)}
my output like this
{"accessID":
{
"_id": "5cef82d1d2f7e14d3013ac8c",
"accessLists":
[
"5cee8e093defe92b88c1626b",
"5cee8e093defe92b88c1626c",
"5cee8e093defe92b88c1626d",
],
"name": "Admin",
},
"name": "ali"
"email": "ali17#yahoo.com"
}
but I want my output like this:
{
"name": "ali",
"email": "ali17#yahoo.com"
"accessID":
{
"_id": "5cef82d1d2f7e14d3013ac8c",
"accessLists":
[
{
"_id": "5cee8e093defe92b88c1626b",
"resource": "groups",
"method": "POST",
},
{
"_id": "5cee8e093defe92b88c1626c",
"resource": "groups",
"method": "DELETE",
},
{
"_id": "5cee8e093defe92b88c1626d",
"resource": "groups",
"method": "GET",
}
]
"name": "Admin",
}
}
User.findById(req.params.userId)
.populate({path:'accessID',model:'accesses'})
.populate({ path: 'accessID', populate: { path: 'access_List',model:'resources' }})
.exec(function (err, response) {
})
I have tried and its working.
More Help: Example
also, I have tried with aggregation :
db.users.aggregate([
{$match : { _id : ObjectId("5cf00388eae5400e9439b2df")}},
{
$lookup: {
from:"access",
localField:"accessID",
foreignField: "_id",
as:"accessID"
}
},
{ "$unwind": '$accessID'},
{ "$unwind": '$accessID.access_List'},
{
$lookup: {
from:"resources",
localField:"accessID.access_List",
foreignField: "_id",
as:"access_List"
}
},
{
$group : {
_id : {
"_id" : '$_id', "name" : '$name', "email" : '$email',
"accessID" : {"_id" : '$accessID._id', "name": '$accessID.name' }
},
access_List : { $push : { "$arrayElemAt": [ "$access_List", 0 ] } }
}
},
{ $project : {
_id : '$_id._id',
name : '$_id.name',
email : '$_id.email',
accessID : {
_id : '$_id.accessID._id',
name: '$_id.accessID.name',
access_List : '$access_List'
}
}
}
]).pretty()

How to increment property's value(integer) inside .update() and $set mongoose?

I'm trying to find a document in my database using findOne() and then search that document for options array that contains objects. Then I check object's property if it's equal to pollOption then I want to increment that object's another property votes by 1, but I can't get that property's value so I can increment it. Please help.
Routes.js
router.post('/submitVote', function(req, res){
const {pollId, pollOption} = req.body;
Polls.findOne({_id: pollId}
).update({'options.option': pollOption}, {'$set': {
'options.$.votes': '', // INCREMENT BY 1 //
}}, function(err){
if(err){
return console.log(err);
} else {
return res.send('success');
}
});
});
Sample Model:
{
"_id": {
"$oid": "5b2ec4852a51d06734f71e79"
},
"options": [
{
"option": "Amazing!",
"votes": 0
},
{
"option": "Good.",
"votes": 0
}
],
"creator": "Guest",
"name": "Rate this website!",
"__v": 0
}
Polls.js - Schema
var mongoose = require('mongoose');
const Poll = new mongoose.Schema({
name: { type: String, required: true },
options: { type: Array, required: true },
creator: { type: String, default: 'Guest' }
});
const Polls = mongoose.model('Polls', Poll);
module.exports = Polls;

How to remove Object from array using mongoose

I'm trying to remove an object from an array in a document using mongoose.
The Schema is the following:
var diveSchema = new Schema({
//irrelevant fields
divers: [{
user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
meetingLocation: { type: String, enum: ['carpool', 'onSite'], required: true },
dives: Number,
exercise: { type: Schema.Types.ObjectId, ref: 'Exercise' },
}]
});
a possible entry can be
{
//irrelevant fields
"divers": [
{
"_id": "012345678",
"user": "123456789",
"meetingLocation": "carpool",
"exercise": "34567890",
},
{
"_id": "012345679",
"user": "123456780",
"meetingLocation": "onSite",
"exercise": "34567890",
}
]
}
Say I want to remove the entry where user is 123456789 (note I do not know the _id at this point).
How do I do this correctly?
I tried the following:
var diveId = "myDiveId";
var userIdToRemove = "123456789"
Dive.findOne({ _id: diveId }).then(function(dive) {
dive.divers.pull({ user: userIdToRemove });
dive.save().then(function(dive) {
//do something smart
});
});
This yieled no change in the document.
I also tried
Dive.update({ _id: diveId }, { "$pull": { "divers": { "diver._id": new ObjectId(userIdToRemove) } } }, { safe: true }, function(err, obj) {
//do something smart
});
With this I got as result that the entire divers array was emptied for the given dive.
What about this?
Dive.update({ _id: diveId }, { "$pull": { "divers": { "user": userIdToRemove } }}, { safe: true, multi:true }, function(err, obj) {
//do something smart
});
I solve this problem using this code-
await Album.findOneAndUpdate(
{ _id: albumId },
{ $pull: { images: { _id: imageId } } },
{ safe: true, multi: false }
);
return res.status(200).json({ message: "Album Deleted Successfully" });
Try this
Dive.update({ _id: diveId },{"$pull": { "drivers": {"user": "123456789"}}})
Try this async code
var diveId = "myDiveId";
var userIdToRemove = "123456789"
const dive=await Dive.findOne({ _id: diveId })
await dive.divers.pull({ user: userIdToRemove });
await dive.save();
Use this with try/catch:
await Group.updateOne(
{ _id: groupId },
{ $pull: { members: {id: memberId }}}
);

Resources