Anuglar same criteria multiple checkboxes - angularjs

I have 3 arrays:
brands = [
{'id': 1, 'name': 'SOME BRAND'}
....
]
sizes [
{'id': 1, length: 12}
....
]
items [
{id: 1, brand: 1, size: 1}
....
]
Brands and sizes become checkboxes. I need to set up filter for items by brands and sizes. I don't have a clue how. Please help.

create your own filter https://docs.angularjs.org/api/ng/filter/filter
and in the callback do the desired stuff.

Related

how to loop my react data, its not showing my data

I makes crud with react but i have trouble when try to displaying from data table. please help me
this my code
my conn to api
this my looping data
looping code
this the console log
console log
I saw that you make an array which contains a lot of arrays. Is this your expectation?
rowsData = [
[{ DocumentID: 1, DocumentName: 2, status: 3, navigation: 4 }],
[{ DocumentID: 1, DocumentName: 2, status: 3, navigation: 4 }]
]

How to invert an existing master-detail relation within a mongodb document by using a query?

I'm just planning to switch from Orders collection structure to Items structure with a query.
Of course I'm able to to make it by iterating and with a few lines coding, but I'm sure there is an easier way to achieve it with a mongo query.
Each document in Orders collection can contain a User and many itemIds.
From this collection I would like to get an Items collection output with a query. What sort of aggregation/projection I need to use to get UserIds linked to each item ?
Source, Orders collection
{
"UserId" : "Acme",
"ItemIds" : [
1,
2,
3
]
},.....
Destination, Items Collection
{
"ItemId" : 1,
"UserIds" : [
1,
3
]
},....
P.S. this is not a DB design question and the number of items in Orders and itemIds are finite. I made up them to explain the problem.
You could use an aggregation pipeline with 3 stages:
$unwind itemIDs
$group by itemID, $push UserId into an array
$out to new collection
Similar to Joe's method, I used this to 'invert' a parent-child relationship:
the Orders data looks like:
[
{ userId: 1, itemIds: [5, 6, 7]},
{ userId: 2, itemIds: [7, 8, 9]},
{ userId: 1, itemIds: [4, 5, 6]},
{ userId: 3, itemIds: [5, 6, 7]}
]
Mongo Query:
db.getCollection('Orders').aggregate([
{$unwind: '$itemIds'},
{$project: {userId: 1, itemIds: 1}},
{$sort: {itemIds: 1, userId: 1}},
{$group: { _id: "$itemIds", userIds: { $push : "$userId"} }},
{$sort: { _id: 1 }}
])
$sort is optional, of course
the result should look like:
[
{ itemId: 4: userIds: [1] },
{ itemId: 5: userIds: [1, 1, 3] },
{ itemId: 6: userIds: [1, 1, 3] },
{ itemId: 7: userIds: [1, 2, 3] },
{ itemId: 8: userIds: [2] },
{ itemId: 9: userIds: [2] },
]
If you want unique userIds
{$group: { _id: "$itemIds", userIds: { $addToSet : "$userId"} }},

Searching for another way of displaying object of object in Mongodb

I have data that looks like:
[
{
'_id': ObjectId('589ba2fb2742a35b47dad21c'),
'name': 'Iphone7',
'price': 14500,
'category': 'Phone',
'vendor': 'Apple',
'stock': [
10,
40,
],
'quantity': 10,
},
{
'_id': ObjectId('589ba2fb2742a35b47dad21d'),
'name': 'Samaung TV',
'price': 6500,
'category': 'TV',
'vendor': {
'name': 'Samaung',
'phone': '01061202200',
},
'stock': [
5,
70,
80,
34,
],
'quantity': 5,
},
];
I could get second "vendor" which contain phone as like:
db.products.find({"vendor.phone": {"$exists": true}}).pretty()
I'm searching for any other way to do get only the vendor that contains "phone" value. I'm new to mongo. thanks in advance.
I would argue $exists is the best. However if for some case you insist not to do so you could use $type to only find documents where vendor.phone is of a certain type.
Under the assumption that all phone numbers are type string you could use this query:
db.collection.find({
"vendor.phone": {
$type: 2
}
})
If vendor.phone can be multiple types you'll have to use an $or query to cover all those types like so: (in this example types 1 and 2 represent number and string)
db.collection.find({
$or: [
{
"vendor.phone": {
$type: 1
}
},
{
"vendor.phone": {
$type: 2
}
}
]
})
Mongo Playground

Best approach to manage related objects in angular?

Let's say I have two json objects, one of users and one of accounts, something like:
users = [
{id: 1, account_id: 1},
{id: 2, account_id: 2},
{id: 3, account_id: 1}
]
accounts = [
{id: 1, name: "administrator"},
{id: 2, name: "moderator"}
]
So I have to loop accross all the users array, and for each of them get the account information. Which is the best way to manage those relationships for accessing them in the markup? I found the following solutions:
Approach 1: Make a repeat of only one element, so that it's just to filter the elements and to make it available in that part of the markup
<div ng-repeat="user in users">
<div ng-repeat="account in getAccountFromId(user.account_id)">
<!-- now account is available in all this scope to access user.account.name -->
</div>
</div>
Approach 2: Change the way the information is returned from the backend, bringing a json of each user where each json is returned with the account information. But that will repeat a lot of informacion in each json object. Also this implies changing the backend because of angular.
<div ng-repeat="user in users">
<!-- with this approach I can access here user.account.name -->
</div>
Could someone tell me if those approachs are correct? Is there a better way to manage the object relationships in angular?
Thanks a lot.
If you really don't like the thought of changing the shape of the data coming from the server, another option is to just map the users to accounts in javascript.
app.controller("MyController", function($scope) {
// Pretend that 'users' and 'accounts' here came from an xhr request
var users = [
{id: 1, account_id: 1},
{id: 2, account_id: 2},
{id: 3, account_id: 1}
]
var accounts = [
{id: 1, name: "administrator"},
{id: 2, name: "moderator"}
]
// Map accounts to users
for(var i=0; i<users.length; i++) {
for(var j=0; j<accounts.length; j++) {
if (accounts[j].id === users[i].account_id) {
users[i].account = accounts[j];
}
}
}
});
I faced the same problem and decided it was not the front-end job to perform data mapping.
So instead of returning on account_id like:
users = [
{id: 1, account_id: 1},
{id: 2, account_id: 2},
{id: 3, account_id: 1}
]
I extended the model with "account_name" (or whatever the equivalent for me)
Hence the suggested output
users = [
{id: 1, account_id: 1, account_name: "administrator"},
{id: 2, account_id: 2, account_name: "moderator"},
{id: 3, account_id: 1, account_name: "administrator"}
]
It is slightly redundant but makes your life easier in the UI and doesn't cost much in the server.

Pull item by value from embedded array in Mongo

I want to pull a specific item from an embedded array...assume the following mongo document....
db.test.find()
{
id:1,
comments :
[
{ cid: 1 },
{ cid: 2 },
{ cid: 3 },
{ cid: 4 },
{ cid: 5 }
]
}
I want to remove an item from the comments array by cid, not by position. I've tried all of these but none of them appear to work. I've tried using the dot notation, but that does not seem to have any effect. I tried the last post suggestion from How to Delete-nth element from array, but no luck...
db.test.update({ 'comments.cid' : 5}, {"$pull" :{"comments":{"cid":"3"}}} )
db.test.update( {id: 1}, {"$pull" : {"comments" : { "cid" : "3"}}},false,false)
db.test.update( {id: 1}, {"$pull" :{"comments.cid" :3}})
this should work:
db.test.update( {id: 1}, {$pull :{comments: {cid :3}}})
also, in your document, you have:
id: 1 without comma at the end, it shold be:
id:1,
These worked too...
db.test.update({comments:{cid:4} },
{$pull:{comments:{cid:4}},
$inc:{commentCount: -1}})
db.test.update({"comments.cid" : 17},
{$pull:{ comments:{cid: 17}},
$inc:{commentCount:-1}})
Just wanted to modify the answer so that it can delete multiple objects from an array.
db.test.update( {id: 1}, {"$pullAll" : {"comments" : [{ "cid" : "3"},{ "cid" : "2"}]}})
This answer has been updated and it works with mongoose too

Resources