Get a single field from a subdocument array in mongodb - javascript

Below is my collection schema...
What I am trying to do is get the value of "eak" from "Website 1" and use it in my application. I am struggling to pull the value out of the multi embedded array.
_id:1234,
role:"client",
organization:"My Organization",
email:"myorg#notarealemail.com",
password:"myEncryptedPassword"
websites:
[{
_id:5554
name:"Website1"
wak:"567567567"
wid:"5678"
preview:"aSweetImage"
cid:"678"
integrations:
[{
integration_data:[]
_id:9876
name:"Integration 1"
img:"Integration Logo"
},
{
integration_data:[]
_id:8765
name:"Integration 2"
img:"Integration Logo"
eak:"12345678"
}]
},
{
_id:5555
name:"Website2"
wak:"567567568"
wid:"5679"
preview:"aSweetImage"
cid:"679"
integrations:
[{
integration_data:[]
_id:9877
name:"Integration 1"
img:"Integration Logo"
},
{
integration_data:[]
_id:8766
name:"Integration 2"
img:"Integration Logo"
eak:"12345679"
}]
}]
I have access to the "_id" for "Website 1" and am currently trying to call the value like this.
Users.find({
eak: { "websites._id": wid, "integrations.name": "Integration 2" },
})
.then((eak) => {
console.log("HERE", eak);
})
.catch((err) => {
console.log(err);
});
I have a feeling that I am simply not fully understanding the way find() works. I have read the mongodb documentation and I am clearly missing something.
the console is logging:
HERE []

There are two problems.
Mongo db us case sensitive. Searching for website will not result Website and Website1 is not same as website 1
If your resolve the above issue, you need to use positional operator to return the matching document.

Related

Find and match string in array object and then sort by object value. React and Javascript

i have an array of nested objects that give each user a permission.
const UserList = [{
0: {
users: {
"email": "user1#gmail.com"
},
permission: {
"id": 1,
"access": "Edit"
}
},
1: {
users: {
"email": "user2#gmail.com"
},
permission: {
"id": 2,
"access": "Read Only"
}
},
2: {
users: {
"email": "user3#gmail.com"
},
permission: {
"id": 1,
"access": "Edit"
}
},
}]
My problem: I want to be able to match a email string to the email in the object and then grab object with the access "read only'. This is all to disable a button. So if the current user's email matches one in the object and the access equals "read only" then pull it out. Im not sure if i want to create a function/prop for this condition but the disable button is in another file.
So lets say this is my email
const myEmail = user2#gmail.com. How do i compare it to UserList and create that condition above. Then transfer it to a button in another file <button disabled={solution goes here}></button>
Edit: I want to find the object that has the current users email, then if that object's access = read only, then disable.
Maybe a .include(myEmail)? Not sure
Thanks for your help!
You can use Object.values(UserList) to get an array with just the values (the user object in this case). Then you can use that list to do your rendering (seems you are using react), so you can just map over the list, and then use the information to check whether a value is `Read Only" etc.
in your HTML, that looks something like:
<button disabled={userObj.permissions.access !== "Read Only"}>
I am not quite following the question about matching email strings.

How to advance mongoose search?

Hello everyone last 16 hours I`m trying to implement this search with mongoose, and unfortunately whatever I try didn't work.
Hope someone here will be able to help me.
So I have schema
const propertySchema = new mongoose.Schema({
city:{
type:String
},
district:{
type:String
},
address:{
type:String
}
});
const Property = mongoose.model('property',propertySchema);
exports.module = Property
And this is my collection (demo) data
{
"city":"Prague",
"district":"district 1 ",
"address":address 1"
},
{
"city":"Prague",
"district":"district 1 ",
"address":address 1"
},
{
"city":"Ostrava",
"district":"district 2 ",
"address":address 2"
},
{
"city":"Ostrava",
"district":"district 2",
"address":address 2"
},
{
"city":"Brno",
"district":"district 3",
"address":address 1" //Let`s pretend somehow this address in Bruno exists in Prague too
},
What I want now is when I write in search "Prague" to get result
Prague
Also when I write for example in search "address 1" to get result
address 1,district 1 , Prague
address 1,district 3 , Brno
I should look similar like https://www.zillow.com search
So far I came up with a lot of idea and this is last (that didn`t work but still I want to share it with you maybe with some modification can make this )
const all = await Property.find({$and:[{city:{$regex: req.body.tag, $options: 'i'}},{address:{$regex: req.body.tag, $options: 'i'}},{district:{$regex: req.body.tag, $options: 'i'}}]})
In this type of scenario, where you want to match search input to any field you have to put $or aggregation instead of $and. Because you want to match input to be either city or district or address.

Firebase - IN query not working in where clause getting blank array as result

When I try to filter my data with IN query it's not showing me the result I am getting a blank array in result I have tried the same thing in the firebase console. I am not getting result there as well.
My data is
messages/01fjubNOf8oYODitr1h6 In this path, I have keys - name, message, participants
{
name: "Pawan arora",
message: "test",
participants: ["33", "127"]
},
{
name: "TEST NAME",
message: "not working",
participants: ["114", "127"]
}
{
name: "TEST DATA",
message: "new",
participants: ["114", "33"]
}
I tried in the query to filter data my query is
.collection("messages").where("participants", "in", ["33", "127"])
Here I should get the first object as a result
Firebase database image
You need to use array-contains-any operator instead when filtering arrays.
Try this:
.collection("messages").where("participants", "array-contains-any", ["33", "127"])
For details check this blogpost.

Is there any way to search for objects with the specific field in array of objects in google-cloud-firestore

I would like to query objects in array of objects with specific field in google cloud firestore,
I have collection structured like this :
...
{
name: "shoes 1",
amount:150,
productors: [
{
id : "a001",
name : "productor 1"
},
{
id: "a002",
name: "productor 2"
}
...
]
...
}
{
name: "shoes 2",
amount:80,
productors: [
{
id : "a002",
name : "productor 2"
},
{
id: "a001",
name: "productor 1"
}
...
]
...
}
...
and I would like to query this collection to get all docs that have in productors array, produtor with the id = e001.
I work in a nodejs environment, using firestore-admin-sdk
This is not possible with Cloud Firestore. You can't query for properties of objects within an array. You can only query an array for the entire value of one of its items (the full object).
For this query, you will need a separate field to query, perhaps array that contains only the id values you want to find. You can use an array-contains type query for that.

How to get a value from nested JSON data

I am trying to get a value from the nested JSON data below. Specifically, the payments captures id value 0TA12948FV40723B is the value I need.
I try using the following codes to retrieve the value.
details.purchase_units.payments.captures.id
details.purchase_units.payments[0].captures.id
But I keep getting a console.log Error: "Order could not be captured"
JSON DATA
{
"create_time":"2019-02-19T05:06:52Z",
"update_time":"2019-02-19T05:06:52Z",
"id":"3HB96413YD922272B",
"intent":"CAPTURE",
"status":"COMPLETED",
"payer":{
"email_address":"a#yandex.com",
"payer_id":"WEJUPTK4U53E9",
"address":{
"address_line_1":"1 Main St",
"admin_area_2":"San Jose",
"admin_area_1":"CA",
"postal_code":"95131",
"country_code":"US"
},
"name":{
"given_name":"a",
"surname":"som"
},
"phone":{
"phone_number":{
"national_number":"408-214-8270"
}
}
},
"purchase_units":[{
"reference_id":"default",
"amount":{
"value":"1.01",
"currency_code":"USD"
},
"payee":{
"email_address":"gr-facilitator#yandex.com",
"merchant_id":"MSOIGVMKKWAMA"
},
"shipping":{
"name":{
"full_name":"Mr T"
},
"address":{
"address_line_1":"1234 Main St.",
"address_line_2":"Unit 1",
"admin_area_2":"Chicago",
"admin_area_1":"IL",
"postal_code":"60652","country_code":"US"
}
},
"payments":{
"captures":[{
"status":"COMPLETED",
"id":"0TA12948FV40723B",
"final_capture":true,
"create_time":"2019-02-20T05:06:52Z",
"update_time":"2019-02-20T05:06:52Z",
"amount":{
"value":"1.01","currency_code":"USD"
},
"seller_protection":{
"status":"ELIGIBLE",
"dispute_categories":[
"ITEM_NOT_RECEIVED",
"UNAUTHORIZED_TRANSACTION"
]}
}
]}
}]
}
Since purchase_units and captures are arrays:
details.purchase_units[0].payments.captures[0].id
captures key have objects in side an array. so you can't simply fetch like
details.purchase_units.payments[0].captures.id
you have to give the position of the captures or loop the array only you can do parsing
details.purchase_units.payments[0].captures[0].id
If you add your json as object using the chrome console you will be able to do what I did in in the image below. purchase_unit is an array so you need to access it using the index.
In case if you want to capture all the IDs in an array, you can use wild card:
details.purchase_units[*].payments.captures[*].id
This will parse complete payload for specified path and collect all the IDs in an array.

Categories