Get key based on a sub-attribute match - javascript

So if I have a tree structured as following in firebase then how can I fetch the unique key where the Name == "Employee 1" and this should return the key "LJrWJlgUPWUaPQx0ok09CswIkXg2" ?
"Users" : {
"LJrWJlgUPWUaPQx0ok09CswIkXg2" : {
"Shifts" : {
"dfaskjhfkashdf" : {
"endTime" : 1490907600,
"startTime" : 1490878800
}
},
"email" : "Employee1#gmail.com",
"employeeId" : "LJrWJlgUPWUaPQx0ok09CswIkXg2",
"fcmToken" : "eOVPmlxy9fc:APA91bEChCT-JwnH14yLrkWBdWBR-KJwj_FznOgwSzu-2JbL2hD5tRTl-7GkavLSihSzJMJ2_f7FjDhUgnU464EKNGaTUjoV0ZuLbJMlxtJeghhRCBDTbkm_J_yiH29IDXHVjmxbfVHw",
"name" : "Employee 1",
"password" : "Employee1",
"startDate" : "2017-01-23"
}

If I understand your question correctly, this ought to do the trick:
firebase.database().ref('Users')
.orderByChild('name')
.equalTo('Employee 1')
.limitToFirst(1)
.once('value', snap => {
let key = Object.keys(snap.val())[0]; // LJrWJlgUPWUaPQx0ok09CswIkXg2
let user = snap.child(key).val(); // the whole user object
});

Related

How to extract data from Firebase realtime database JSON-export

I have a Firebase realtime-database export-json that has nested informations, that I would like to extract. The structure of the JSON looks like this:
{
"users" : {
"024w97mv8NftGFY8THfQIU6PhaJ3" : {
"email" : "xxx",
"id" : "024w97mv8NftGFY8THfQIU6PhaJ3",
"name" : "xxx",
"items" : {
"-LQL9n-r9BGdo3HJZ2sk" : {
"disliked" : true,
"id" : 396,
"name" : "Aaa"
},
"-LQL9oO63nH-QW2w6zz0" : {
"liked" : true,
"id" : 3674,
"name" : "Bbb"
}
}
},
"0ERLT5DLRvbZUnjlnM7Ow0qItpz2" : {
"email" : "zzz",
"id" : "0ERLT5DLRvbZUnjlnM7Ow0qItpz2",
"name" : "zzz",
"items" : {
"-LIZnriSVQMzqTsPFNYa" : {
"id" : 396,
"liked" : true,
"name" : "Aaa"
},
"-LIZnrzOuk4WyjqEiLG8" : {
"disliked" : true,
"id" : 4805,
"name" : "Ccc"
}
}
}
}
}
What I need to achieve is getting a list of all liked item-names, and ideally counting how often an item is liked.
Is there a simple tool or script to do that? Ruby or Javascript would be preferred. Thanks a lot!
You can parse your JSON data in ruby like below
result_hash = JSON.parse(result)
result_ary = result_hash["users"].collect do |k,v|
v["items"].values.select{|v1| v1["liked"] == true }
end
result_data = result_ary.flatten
result of parsing
=> [{"liked"=>true, "id"=>3674, "name"=>"Bbb"}, {"id"=>396, "liked"=>true, "name"=>"Aaa"}]
Now its very easy for getting your required result
result_data.collect{|x| x["name"] }
=> ["Bbb", "Aaa"]
result_data.count {|x| x["name"] == "Aaa"}
=> 1
result_data.count {|x| x["name"] == "Bbb"}
=> 1

How to get slug value from array of objetcs if match with provide id?

I'm trying to get the slug value from the array if inside the object.extra.services array, one of the element makes match with the id im providing...
// Service ID provided
const serviceID = '5cdd7c55f5abb90a689a44be';
// Array of services Ids
getProductsServices(products) {
const productsServices = [
...new Set(products.map(product => product.extra.services))
];
const productsList = [].concat.apply([], productsServices);
return productsList;
},
// ServiceId Matching
serviceMatch(serviceID) {
return this.getProductsServices.includes(serviceID);
}
Now i need to get the slug value inside the array that match the service id provided.
products [{
"_id" : ObjectId("5e0257dcbe760674b10d4122"),
"desc" : "Diseño de Pagina Web",
"extra" : {
"image" : "/2018/06/diseño-de-logos-para-empresas.jpg",
"services" : [
"5cdd7c55f5abb90a689a44be",
"5cdd7c55f5abb90a689a3fcc",
"5cdd7c55f5abb90a689a3f42"
]
},
"name" : "Diseño de logo",
"slug" : "diseno-de-logotipos-online"
},
{
"_id" : ObjectId("5e0257dcbe760674b10d4122"),
"desc" : "Diseño de logo",
"extra" : {
"image" : "/2018/06/diseño-de-logos-para-empresas.jpg",
"services" : [
"5cdd7c55f5abb90a689a44be",
"5cdd7c55f5abb90a689a3fcc",
"5cdd7c55f5abb90a689a3f42"
]
},
"name" : "Diseño de logo",
"slug" : "diseno-de-logotipos-online"
},
{
"_id" : ObjectId("5e0257dcbe760674b10d4122"),
"desc" : "Diseño de Interior",
"extra" : {
"image" : "/2018/06/diseño-de-logos-para-empresas.jpg",
"services" : [
"5cdd7c55f5abb90a689a44be",
"5cdd7c55f5abb90a689a3fcc",
"5cdd7c55f5abb90a689a3f42"
]
},
"name" : "Diseño de logo",
"slug" : "diseno-de-logotipos-online"
}]
Try includes:
const found = products.find(product => product.extra.services.includes("5cdd7c55f5abb90a689a3fcc"))
if (found) {
console.log('found', found)
}
If I understood correctly, you can achieve it using find along with some:
const product = productsList.find(product => product.extra.services.some(id => id === serviceID))
console.log(product.slug)

Get nest object by ID in MongoDB document

I want to retrieve a comment in a post to edit. I'm not sure how to approach this.
Here's what my Post document looks like:
{
"title" : "First Node.js App",
"body" : "testing 123",
"status" : "public",
"user" : "John Doe",
"date" : ISODate("2017-12-21T18:30:09.779Z"),
"comments" : [
{
"commentBody" : "This is awesome! ",
"commentUser" : ObjectId("5a3bfd5a9e65351f9c18ba18"),
"_id" : ObjectId("5a3c02379e65351f9c18ba1a"),
"commentDate" : ISODate("2017-12-21T18:49:27.620Z")
},
{
"commentBody" : "This is second comment.",
"commentUser" : ObjectId("5a3bfd5a9e65351f9c18gt19"),
"_id" : ObjectId("5a3c02379e65351f9c18ba1b"),
"commentDate" : ISODate("2017-12-21T18:49:27.620Z")
}
],
"allowComments" : true
}
How do I retrieve comment id ObjectId("5a3c02379e65351f9c18ba1a")?
I've tried the following but had no luck:
const post = await Post.findById(req.params.id);
const comment = post.comments.find({"_id": req.params.commentid});
You need to pass a callback to Array#find:
const comment = post.comments.find((el) => el._id === req.params.commentid);

Query in firebase usining angular 4 and angularFire 2

I try to query a list from 5000 products that have property = 110571 is not do anything
I use angularfire 2 but for the last 5 hours, I don't get anything help me.
What is the best way to do that?
This is the object in Firebase database
"products" : {
1753: {
"name" : "sprite",
"custom_id" : 534253,
"description" : "some small description"
},
1754: {
"name" : "coca cola",
"custom_id" : 110571, <---- i want to find if this node from 5000 products exist in database and if exist i want to return true
"description" : "some small description"
},
1755: {
"name" : "fanta",
"custom_id" : dsgfsdfds,
"description" : "some small description"
},
}
This is the function that I use
x = 1754;
check_if_product_code_exist(x) {
console.log(x);
this.searchSubject.next(x);
// this.products = this.db.list('/products') as FirebaseListObservable<Product[]>;
this.products = this.db.list('/products', { query: {
orderByChild: 'custom_id',
equalTo: this.searchSubject,
limitToFirst: 1
}} ) as FirebaseListObservable<Product[]>;
console.log(this.products);
return this.products;
}

MongoDB .find() only ObjectId's embedded in an Array.

I am trying to get only the ObjectId's from One specific Document that is embedded in the projects Array.
Basically I am trying to make a database that will have users and each user will have there own projects.
Thank you !
db.users.find().pretty()
{
"_id" : ObjectId("5762c0cf2b9a78006373a684"),
"name" : "seq",
"pass" : "seq",
"projects" : [
{
"pid" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},
{
"pid" : ObjectId("5762c0ba2b9a78006373a683"),
"name" : "bbb"
}
]
}
{
"_id" : ObjectId("5762c28d2b9a78006373a687"),
"name" : "leq",
"pass" : "leq",
"projects" : [
{
"pid" : ObjectId("5762c2892b9a78006373a685"),
"name" : "ccc"
},
{
"pid" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
]
}
let say we want two pids
{"pid" : ObjectId("5762c0ba2b9a78006373a682")} and
{"pid" : ObjectId("5762c2892b9a78006373a686"),}
and only inner documents
so required response should look like:
{
"_id" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},{
"_id" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
Aggregation framework can manipulate documents, match only needed ones and transform inner structure by project phase:
var match = {
$match : {
"projects.pid" : {
$in : [ObjectId("5762c0ba2b9a78006373a682"),
ObjectId("5762c2892b9a78006373a686")]
}
}
}
var unwind = {
$unwind : "$projects"
};
// now move array objet as top level object
var project = {
$project : {
_id : "$projects.pid",
name : "$projects.name",
// list other fields here
}
}
db.vic.aggregate([match, unwind, match, project])

Categories