Getting only needed elements form array in MongoDb Schema - javascript

I want to get only those element from notes Array that match the given tag from tags array in notes.
For example : If I want to get only those notes from notes Array having lrn tag in tags array but I am getting either the whole object or only the first matching note.
My Schema :
const mongoose = require('mongoose');
const noteSchema = new mongoose.Schema({
user : {
type: String,
required: true,
unique : true
},
notes : {
type : [{
title : {
type: String,
required: true
},
tags : {
type : [String],
required: true
},
note: {
type: String,
required: true
}
}],
required: true
}
});
const Notebook = mongoose.model('NOTEBOOK', noteSchema);
module.exports = Notebook;
My sample data :
{ _id: ObjectId("621c3a41fa2fe3c07f43edc9"),
user: '620c81434d8a65c1aa36e0d4',
notes:
[ { title: 'Eraning',
tags: [ 'ern', 'lrn', 'dik', 'pik', 'sik' ],
note: 'Prior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.\nPrior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.\n',
_id: ObjectId("62205a32bab7aed6315195e9") },
{ title: 'Lerningngs',
tags: [ 'lrn', 'eat', 'drink' ],
note: 'Prior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.\n',
_id: ObjectId("6220583cbab7aed63151958b") },
{ title: 'Learning',
tags: [ 'learn', 'eran', 'buy', 'sell' ],
note: 'Prior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.Solving DSA from Maths.in and SolveMaths.org using great techniques by INDIA and RUSSIA.\n',
_id: ObjectId("6220532dbab7aed631519531") },
{ title: 'Biology',
tags: [ 'tissue', 'cell', 'myto', 'energy', 'glycogen', 'hydrogen' ],
note: 'Prior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.Solving DSA from Maths.in and SolveMaths.org using great techniques by INDIA and RUSSIA.\n\n',
_id: ObjectId("621f88d4c189c6b5501c3d5f") },
{ title: 'Chemistry',
tags: [ 'this', 'is', 'great', 'thing', 'to', 'do', 'by', 'time' ],
note: ' Prior to his return, Craig worked at NeXT, followed by Apple, \n and then spent a decade at Ariba, an internet e-commerce pioneer \n where he held several roles including chief technology officer.\n',
_id: ObjectId("621f85e9c189c6b5501c3d49") },
{ title: 'Physics',
tags:
[ 'newtonlawsofmotion',
'ktg',
'thermo',
'fluid',
'mechanice',
'bernaulii' ],
note: 'P\' + hrg + 1/2rv^2 = constant',
_id: ObjectId("621f8520c189c6b5501c3d3f") },
{ title: 'Maths',
tags: [ 'trignometry', 'llp', 'continuity', 'tags', 'circle' ],
note: 'Solving DSA from Maths.in and SolveMaths.org using great techniques by INDIA and RUSSIA.',
_id: ObjectId("621f84d7c189c6b5501c3d37") },
{ title: 'DSA from Net',
tags: [ 'dp', 'stack', 'queue', 'heaps', 'sorting' ],
note: 'Solving DSA from Leetcode.in and GeeksforGeeks.org',
_id: ObjectId("621c3a41fa2fe3c07f43edca") } ],
__v: 7 }

You can use $filter to get documents of given tag as shown below:
Notebook.findOne({
user: "620c81434d8a65c1aa36e0d4" // user ID
},
{
notes: {
$filter: {
input: "$notes",
as: "note",
cond: {
$in: [
"ern", // Tag to search
"$$note.tags"
]
}
}
}
})
Mongo Playground

Related

Storing an Array of String to a Key in MongoDB

I'm building a post request which will contain various info on each role. E.g in this trial, 'Data Analyst'.
In this position I want to have quite a few sources of data, including things like salary, education and skills.
However, I'm using recharts to visualise it, which means although I need a key, e.g Masters's degree, I also need a num value associated, such as below:
const educationData = [
{
degree: 'Masters',
A: 120,
},
{
degree: 'Bachelor',
A: 98,
},
{
degree: 'PhD',
A: 86,
},
{
degree: 'Industry',
A: 99,
},
{
degree: 'Associate',
A: 85,
}
];
I would like to store this inside my schema created:
const RolesPositionSchema = new mongoose.Schema(
{
title:{type:String, required: true, unique: true},
desc:{type:String, required: true},
skills:{type:Array, required: true},
salary:{type:Array},
education: {type: Array},
popularity: {type: Number},
reccomendations: {type: Array},
},
);
Within this I would like to store the 'educationData' into the DB. Happy to input it in manually for now. I just want to know how to structure my schema/data in the postman request.
E.g Input in "education", stored with "Masters:120", "Bachelor:98", "PhD:86" etc -> within the post request:
{
"title" : "Data Analyst",
"desc": "Using both internal and external data sources you will develop insights to identify trends and opportunities whilst highlighting areas or improvement to optimise member interaction. This is a proactive role where you will own how we interpret data, allowing you to provide valuable insight into the development and implementation of product, customer and channel strategies for our sales, retention, and acquisition goals.",
"skills":["SQL" ,"Excel", "Tableau", "PowerBI", "Python", "Azure", "AWS", "ETL"],
"education": ["INPUT AN ARRAY OF EDUCATION AND THEIR VALUES HERE!]
}
Thankyou!
You could modify first your payload into the format of schema.
Payload
const educationData = [
{
degree: 'Masters',
A: 120,
},
{
degree: 'Bachelor',
A: 98,
},
{
degree: 'PhD',
A: 86,
},
{
degree: 'Industry',
A: 99,
},
{
degree: 'Associate',
A: 85,
}
];
Process
const newEducations = educationData.map(education => {
return `${education.degree}:${education.A}`;
});
const storeRole = new RolesPositionSchema({
title: 'Data Analyst',
desc: 'Using both internal and external data sources you will develop insights to identify trends and opportunities whilst highlighting areas or improvement to optimise member interaction. This is a proactive role where you will own how we interpret data, allowing you to provide valuable insight into the development and implementation of product, customer and channel strategies for our sales, retention, and acquisition goals.',
skills: [{Skill value}],
salary: [{Salary value}],
education: newEducations,
popularity: 0,
reccomendations: [{Recommendation value}]
});
After this saved.
Does it that you want?

Split object into multiple ordered arrays based on identity of 1 spesific value

Not quite sure if that title was the best I could do.
I'm a pretty new to js and keep running into problems ... I hope some of you have the time to give me a pointer or two on this scenario.
I have several objects that looks pretty much like this - except from the fact that there are 28 instances of every "room" type. I need to split this object into multiple objects - one for each "room" type. In some of my objects there are only one room type - whilst in others there are 3 or 4.
[ { id: 1
created: 2018-12-29T13:18:05.788Z,
room: 'Double Room'
type: 'Standard'
price: 500
},
{ id: 29
created: 2018-12-29T13:18:05.788Z,
room: 'Twin Room'
type: 'Standard'
price: 500
},
{ id: 58
created: 2018-12-29T13:18:05.788Z,
room: 'Family Room'
type: 'Standard'
price: 900
},
]
Oh, and it's important that the instances don't "loose" their order in the array - since it's date related and need to be presentet in an ascending order. And vanilla js only.
Is array.map() the function I'm looking for to solve this problem? Is it posible to do this without iteration?
My final goal is to create some kind of generic function that can sort this out for all my objects.
And guys: happy hollidays!
You could take an object as hash table for the wanted groups. Then iterate the objects and assign the object to the group. If the group does not exist, create a new group with an array.
function groupBy(array, key) {
var groups = Object.create(null);
array.forEach(o => (groups[o[key]] = groups[o[key]] || []).push(o));
return groups;
}
var data = [{ id: 1, created: '2018-12-29T13:18:05.788Z', room: 'Double Room', type: 'Standard', price: 500 }, { id: 29, created: '2018-12-29T13:18:05.788Z', room: 'Twin Room', type: 'Standard', price: 500 }, { id: 58, created: '2018-12-29T13:18:05.788Z', room: 'Family Room', type: 'Standard', price: 900 }],
groupedByRoom = groupBy(data, 'room');
console.log(groupedByRoom);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Complex Mongo query, checking simultaneously on several attributes

Here is what my database could look like :
[
{
_id : xxx,
languages : [
{ lang: "French", level: 1 },
{ lang: "English", level: 3 },
{ lang: "Spanish", level: 4 }
]
},
{
_id : yyy,
languages : [
{ lang: "French", level: 5 },
{ lang: "English", level: 2 }
]
}
]
I have that kind of list :
[
{
lang : "French",
cmp : "at least",
level : 3
},
{
lang : "English",
cmp : "at most",
level : 2
}
]
My goal is to build a query that, with this example :
Select all the users who speaks French with a level >=
AND who speaks English with a level <= 2
the selected users can have others languages for which I do not care
In other words, I want to build a query that finds every users who have ALL the languages specified in the list, each languages having to match the level comparison to be valid.
It's kinda hard, as I'm not used to such complex queries in MongoDB.
Currently, I'm not looking for level comparison. I just query for the users matching the good languages, no matter the level, using "profile.languages.lang" : { $all: languagesArray }, with languagesArray a list of strings I get with a .map on my comparison's object list.
My problem is that I don't know how to spevify so much constraints on a single attribute / list of my document. Of course, I could fetch to refine my search, but it would be really costly over time, as my database is growing pretty fast.
Could anyone guide me ?
Thanks.
Users.find({
$or: [
{
languages: {
$elemMatch: {
lang: 'French',
level: { $gte: 2 }
},
}
},
{
languages: {
$elemMatch: {
lang: 'English',
level: { $lte: 2 }
}
}
}
]
}).fetch();

Appending an entry in one to many embedded document mongodb

I am new to MongoDB and I came across the following use case:
Lets say I have my mongodb document like this:
{
_id: "joe",
name: "Joe Bookreader",
numbers: [
{
mobile: 741134217,
},
{
home: 123452411
}
]
}
Now I need to two perform two operations:
Add a new number {office:112342282}
Delete users home number
I believe that we can do this in Mongo, but I am not getting the syntax anywhere, neither I could find it in the MongoDB documentation.
P.S. I am doing this using Monk Library, monk specific syntax would be of great help. But otherwise also it would help me!
What you want is Mongo's $pull and $push operator
You should be able to do it by doing the following:
db.User.update({_id: 'joe'}, {$push: {numbers: {office: 112342282}}, $pull: {numbers: {home: 123452411}}});
Unfortunately, Mongo doesn't let you operate on the same field with both the $push and $pull operators at the same time (see this issue). So it really needs to be:
db.User.update({_id: 'joe'}, { $push: { numbers: { office: 112342282}}})
db.User.update({_id: 'joe'}, { $pull: { numbers: {home: 123452411}}})
Using monk's style:
var users = db.get('users');
users.update({_id: 'joe'}, { $push: { numbers: { office: 112342282}}})
users.update({_id: 'joe'}, { $pull: { numbers: {home: 123452411}}})

Prevent Javascript function running out of memory because too many objects

I'm building a web scraper in nodeJS that uses request and cheerio to parse the DOM. While I am using node, I believe this is more of a general javascript question.
tl;dr - creating ~60,000 - 100,000 objects, uses up all my computer's RAM, get an out of memory error in node.
Here's how the scraper works. It's loops within loops, I've never designed anything this complex before so there might be way better ways to do this.
Loop 1: Creates 10 objects in array called 'sitesArr'. Each object represents one website to scrape.
var sitesArr = [
{
name: 'store name',
baseURL: 'www.basedomain.com',
categoryFunct: '(function(){ // do stuff })();',
gender: 'mens',
currency: 'USD',
title_selector: 'h1',
description_selector: 'p.description'
},
// ... x10
]
Loop 2: Loops through 'sitesArr'. For each site it goes to the homepage via 'request' and gets a list of category links, usually 30-70 URLs. Appends these URLs to the current 'sitesArr' object to which they belong, in an array property whose name is 'categories'.
var sitesArr = [
{
name: 'store name',
baseURL: 'www.basedomain.com',
categoryFunct: '(function(){ // do stuff })();',
gender: 'mens',
currency: 'USD',
title_selector: 'h1',
description_selector: 'p.description',
categories: [
{
name: 'shoes',
url: 'www.basedomain.com/shoes'
},{
name: 'socks',
url: 'www.basedomain.com/socks'
} // x 50
]
},
// ... x10
]
Loop 3: Loops through each 'category'. For each URL it gets a list of products links and puts them in an array. Usually ~300-1000 products per category
var sitesArr = [
{
name: 'store name',
baseURL: 'www.basedomain.com',
categoryFunct: '(function(){ // do stuff })();',
gender: 'mens',
currency: 'USD',
title_selector: 'h1',
description_selector: 'p.description',
categories: [
{
name: 'shoes',
url: 'www.basedomain.com/shoes',
products: [
'www.basedomain.com/shoes/product1.html',
'www.basedomain.com/shoes/product2.html',
'www.basedomain.com/shoes/product3.html',
// x 300
]
},// x 50
]
},
// ... x10
]
Loop 4: Loops through each of the 'products' array, goes to each URL and creates an object for each.
var product = {
infoLink: "www.basedomain.com/shoes/product1.html",
description: "This is a description for the object",
title: "Product 1",
Category: "Shoes",
imgs: ['http://foo.com/img.jpg','http://foo.com/img2.jpg','http://foo.com/img3.jpg'],
price: 60,
currency: 'USD'
}
Then, for each product object I'm shipping them off to a MongoDB function which does an upsert into my database
THE ISSUE
This all worked just fine, until the process got large. I'm creating about 60,000 product objects every time this script runs, and after a little while all of my computer's RAM is being used up. What's more, after getting about halfway through my process I get the following error in Node:
FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory
I'm very much of the mind that this is a code design issue. Should I be "deleting" the objects once I'm done with them? What's the best way to tackle this?

Categories