I wasnt exactly sure what i should name this question so feel free to edit the title.
I am running an angular application and using node.js combinded with express and sequlize (for my ORM)
Now as many of you may know when your working with an ORM you want to collect Objects inside of another Object for instance a user object might have a country object inside of it as shown below:
user = {id: 222, username: 'Marc', name: 'Marc Ras', country: {id: 1, code: 'DK', name: 'Denmark'}}
Now this is great. binding all of these objects together makes it alot easier to work with in the frontend.
However the other way around is rather annoying.
Lets for the purpose of demonstration take the above object. but lets add a property an array of items
user.items = [{id: 1 , name:'item1'}, {id: 2, name: 'item2'}]
Now if i wish to add this object to my database i would send the combined object to my express route:
var user = {
id: 222,
username: 'Marc',
name: 'Marc Ras',
country: {id: 1, code:'DK', name: 'Denmark'}
items: [
{id: 1, name: 'item1'},
{id: 2, name: 'item2'}
]
}
And now things get abit messy
Say for instance you have the following table:
user_has_items
Each record in this has an id, user_id, item_id
Now in order to make sure that the objects inside user.items match the table you will now have to make code gymnastic by making loops and editing the above object.
Like as such:
user.items.forEach(function (x) {
x.item_id = x.id;
x.id = null; // To make sure the database does not set the id (since this is auto inc)
x.user_id = user.id;
})
Note the above is of course an example it gets more complicated the bigger your object is!
This process leads to messy code (atleast in my opinion) and since this is not the first time i encounter this problem i was wondering if any of you had any solutions as to what you might do to make this process easier?
I have been looking at lodash to see if they had some way of doing this but sadly i could not find anything that made this easy
Related
I have an array of objects, where each object have it's own "_id".
I am trying to use updateMany(), instead calling updateOne for each object, so I don't make too many requisitions to mongoDB.
for example, this is the object:
[
{
_id: 123,
name: "apple",
qty: 2
},
{
_id: 555,
name: "melon",
qty: 1
},
{
_id: 799,
name: "avocado",
qty: 0
}
]
I want to updateMany() with this array of objects, and each object must be a Document inside a collection, I tried many solutions, and none worked, only worked with insert() where I can send the entire array, and it will make each object be a Document, the problem for using insert is, this object comes every day, and some values may change, like name and qty, but _id keeps the same.
I tried the following
sync function updateMany(client) {
result = await client.db("sample_db").collection("CollectionA")
..updateMany({},
{asd},
{upsert: true});
}
but didn't work, I tried other solutions to, like, making each object use $setOnInsert for _id and $set the rest..
Is it possible to do it without the need of insert() or making one call for each object?
The language I am using is Javascript, running on Node.JS.
I'm having trouble wrapping my head around the below syntax in a mongoose schema.
tokens:[{
token:{
type: 'String',
required: true
}
}]
Normally when I want to add objects to arrays I would just push the object. That includes if I want to push an object with an internal object to said array like the example below.
let obj =[]
obj.push({name: 'dirk', age: 24})
obj.push({name: 'chris', age:29, clothes: {shirt: 'black', pants: 'jeans'}, height: '6ft'})
So im confused in mongoose as to why I need this syntax [{}] to use an array?
Ok I'll try to explain this as best I can. In basic JavaScript, an array is just a bucket of "stuff" for lack of better words. What that means is, you could have something like this:
let myList = []
myList.push({name: "John Doe", age: 20})
myList.push({car: {make: "Honda", model: "Accord"}})
myList.push({menu_items: ["item 1", "item 2", "item 3"]})
And JavaScript won't really care right? As far as it's concerned, you haven't done anything wrong because you technically did the correct thing, put some object into a list that can hold whatever you want.
Mongoose, which is an ORM (check out the term if you haven't heard of it before), requires things be a little more strict. Remember that Mongoose is trying to map documents from the Mongo database to this "Object" that should be standard and readable from any point in the code the same what. So in the example in your question listed above:
tokens:[{
token:{
type: 'String',
required: true
}
}]
you are saying to Mongoose that it should expect tokens to contain a list of token objects that have a certain design to them. Now, when you want to add more token objects to the tokens list, you can do something like this:
token = {
// YOUR CORRECT TOKEN DATA
}
tokens.push(token);
If you tried to do something like this instead:
token = {
// RANDOM WRONG DATA
}
tokens.push(token);
Mongoose wouldn't take kindly to it because you are violating the constraints you told Mongoose to keep in affect. If you start throwing any: [{}] into the mix, you are telling Mongoose that any old object will do for that list. This is, in my opinion, very dangerous to do because if you have two types of objects in your list:
var schema1 = new Schema({
name: String,
age: { type: Number, min: 18, max: 65 },
living: Boolean
})
var schema2 = new Schema({
name: String,
age: { type: Number, min: 18, max: 65 },
breed: Boolean
})
and you were working with a list that combined these two objects, if you tried to grab say breed from the schema1 type object, you would either get an error or an odd value that could break your code. So Mongoose strictly types the objects you are pushing to a list, unless you use something like Mixed or any. Here's a link to the documentation that may help explain a bit as well
https://mongoosejs.com/docs/schematypes.html#arrays
If this doesn't make sense or I explained it poorly or answered the wrong question please comment and I'll fix it to explain as best I can.
Happy coding :-)
There are two array of objects one from database and one from csv. I required to compare both array object by their relative properties of Phones and emails and find duplicate array among them. Due to odd database object structure I required to compare both array with Javascript. I wanted to know what is the best algorithm and best way of compare and find duplicates?
I explain simple calculations.
There are 5000 contacts in my database and user may upload another 3000 contacts from csv. Everytime we requires to find duplicate contacts from database and if they find then it may overwrite and rest should be insert. If I compare contact row by row then it may loop 5000 database contacts x 3000 csv contacts = 15000000 time traverse.
This is my present scenario I face due to this system goes stuck. I require some efficient solution of this issue.
I develop the stuff in NodeJS, RethinkDB.
Database object structure exactly represent like that way and it may duplicate entry of emails and phones in other contacts also.
[{
id: 2349287349082734,
name: "ABC",
phones: [
{
id: 2234234,
flag: true,
value: 982389679823
},
{
id: 65234234,
flag: false,
value: 2979023423
}
],
emails: [
{
id: 22346234,
flag: true,
value: "test#domain.com"
},
{
id: 609834234,
flag: false,
value: "test2#domain.com"
}
]
}]
Please review fiddle code, if you want: https://jsfiddle.net/dipakchavda2912/eua1truj/
I have already did indexing. The problem is looking very easy and known in first sight but when we talk about concurrency it is really very critical and CPU intensive.
If understand the question you can use the lodash method differenceWith
let csvContacts = [] //fill it with your values;
let databaseContacts = .... //from your database
let diffArray = [] //the non duplicated object;
const l = require("lodash");
diffArray = l.differenceWith(csvContact,
databaseContacts,
(firstValue,secValue)=>firstValue.email == secValue.email
I have looked what seems everywhere for this but maybe it is too obvious and no one mentions it but I am making a windows 8 app and I want the user to be able to edit the data.
Lets say im trying to make a To-Do list, I want the user to be able to add entries. For my current app with dummy static data I have these entries stored in an array in a javascript file. Should I just make the user be able to add/edit/remove entries in the array or is there a different method I should use?
If someone could link me some material to read or an example that shows what I am looking for it would be really helpful.
It really depends on the JavaScript framework you're using. Assuming that you're using Microsoft's own WinJS, what you're looking for is "data binding". This allows you to bind an object or an array to a "control" and have it be automatically updated. Then you just work with the array, adding, removing, sorting items. The data-bind automatically reflects those changes in the template.
Here is a quick example of data-binding from MSDNs own documentation:
<div id="listDiv"
data-win-control="WinJS.UI.ListView"
data-win-options="{ itemDataSource : dataList.dataSource }">
</div>
<script type="text/javascript">
var dataArray = [
{ name: "Marley", species: "dog" },
{ name: "Lola", species: "cat" },
{ name: "Leo", species: "dog" },
{ name: "Izzy", species: "cat" },
{ name: "Ziggy", species: "cat" },
{ name: "Ruby", species: "dog" }
];
var dataList = new WinJS.Binding.List(dataArray);
WinJS.UI.processAll();
</script>
You can see more here: http://msdn.microsoft.com/en-us/library/windows/apps/hh700774.aspx
Each JavaScript framework does this a little bit differently. For example, AngularJS would let you bind an array to a list like this:
<ul>
<li ng-repeat="item in list">{{item.name}}</li>
</ul>
Where 'list' is the name of an array of objects in your scope, item is a single object in the array, and item.name is a string with the name of a single object. The syntax is different, but once again, you would simply manipulate the array and it would be reflected in the resulting HTML.
I've got a question,
I've got an array in Javascript that looks like this:
var plans = [{
'advanced':[{
'price':'10',
'name':'Advanced'
}],
'basic':[{
'price':'20',
'name':'Basic'
}]
}];
And there's a variable called plan, this could be advanced or basic etc. Now I want to display the price from the array that corresponds with the variable plan. How do I get the price?
I've tried things like:
plans.advanced.price
plans['advanced']['price']
plans[0].advanced['0'].price
plans[0]['advanced']['0']['price']
PS: I am originally A PHP developer, maybe my PHP influence is blocking the correct thoughts, i dont know...
Access it like this: plans[0].advanced[0].price
That's the wrong way to be going about it though; just use JavaScript objects:
var plans = {
advanced: {
price: '10',
name: 'Advanced'
},
basic: {
price: '20',
name:' Basic'
}
};
Then you can access it using plans.advanced.price, which is much more clear.
Note that plans.advanced.price is the same as plans['advanced']['price'] in JavaScript, there is no difference.
Arrays in JavaScript are just glorified objects - as such there is no such thing as "associative arrays" in JavaScript, objects perform the same thing as associative arrays in PHP.
you have some extra array cruft, where you have arrays with one item in them instead of just having the object.
var plans = {
advanced: {
price: '10',
name: 'Advanced'
},
basic: {
price: '20',
name:' Basic'
}
};
if you have var plan ="advanced" you can just do plans[plan].price
if you NEED the current structure with the arrays then it is essentially the same thing but
var plan ="advanced"
plans[0][plan][0].price
hope that helps