Firebase structure:
{
"config" : [
{
"config1" : {
"hideimage" : true
}
},
{
"config2" : {
"hideimage" : false
}
}
]
}
Database rules:
"config": {
".indexOn": ["hideimage"]
}
I'm trying to retrieve all config items that have hideimage attribute set to true using:
admin.database().ref('config').orderByChild("hideimage").equalTo(true).once('value', result => {});
The expected result should be:
[{
"config1" : {
"hideimage" : true
}
}]
but I'm retrieving a null response without getting any error.
Your data structure contains two nested levels:
you have an array
inside the first array element, you have config1 and config2
You can see this if you look at the Firebase console, where your data will show like:
{
"config" : {
"0": {
"config1" : {
"hideimage" : true
}
},
{
"config2" : {
"hideimage" : false
}
}
}
}
Firebase can only query nodes in a flat list, not a tree. So with your current data structure it can only find the node with hideimage=true under /config/0, not under all /config children.
Since you're already naming your config1 and config2 uniquely, I think the array may be a mistake, and you're really looking for:
{
"config": {
"config1" : {
"hideimage" : true
},
"config2" : {
"hideimage" : false
}
}
}
With this data structure your query will work.
Related
I wrote a mongo shell script to insert in a new collection the result of an aggregation; my problem is that the null values from the source collection are replaced by zero; the aggregation part is:
db.getCollection("unhcr_pop_concern_flat").aggregate(
[
{
"$group" : {
"_id" : {
"country" : "$country",
"time" : "$time",
"origin": "$origin"
},
"value_fields": {
"$addToSet": {
k: "$type",
v: {$ifNull: ["$value", null]}
}
}
}
},
{ $addFields: { value_fields: { $arrayToObject: "$value_fields" } } }
],
{
"allowDiskUse" : true
}
Where is the problem? I have also used the $ifnull operator.
The query is working correctly; there was a problem in the source collections where data were changed by others!
I have the following structure in mongodb document
Note : Mongodb Version 3.4
{
"_id" : "1",
"settings" : {
"shapes":[
{
"shape":"square",
"color":"blue"
},
{
"shape":"circle",
"color":"red"
},
{
"shape":"round",
"color":"black"
}
]
}
}
Here i want to update the shape square & round, because value array i am having this two shapes.below code i tried but it is not working my expected.
my code
var value= [
{
"shape":"square",
"color":"yellow"
},
{
"shape":"round",
"color":"blue"
}
]
db.Colleges.update({"_id" : "1"},{
$set : {
"settings.shapes": value
}
},{multi:true})
After update i need this output
{
"_id" : "1",
"shapes":[
{
"shape":"square",
"color":"yellow"
},
{
"shape":"circle",
"color":"red"
},
{
"shape":"round",
"color":"blue"
}
]
}
I am getting output
{
"_id" : "1",
"shapes":[
{
"shape":"square",
"color":"yellow"
},
{
"shape":"round",
"color":"blue"
}
]
}
actually here shape circle and color red is getting disappear, kindly any one update my code.
var value array not a static array, it is dynamic based on objects we to update the document.
I have this data on my firebase:
transportation: {
"car" : {
"bus" : {
"toyota" : true,
"bmw" : true
},
"suv" : {
"honda" : true,
"toyota" : true,
}
}
}
I want to delete all "toyota" data so that my data looks like this:
transportation: {
"car" : {
"bus" : {
"bmw" : true
},
"suv" : {
"honda" : true,
}
}
}
The best possible way to do is check the value using loop in Javascript manually and delete if it's "toyota".
var firebaseRef= firebase.database().ref('transportation/');
firebaseRef.on("value", function(data)){
var datKey=data.key;
firebase.database().ref('transportation/'+datKey).on("value",function(childData)){
var childDataKey=childData.key;
firebase.database().ref('transportation/'+datKey+'/'+childDataKey+'/').on("value",function(data)){
firebase.database().ref('transportation/'+dataKey+'/'+childDataKey+'toyota').remove();
}
}
}
I'm trying to update objects within an array in a document. The objects are not subdocuments and therefore don't have a unique ID. I've tried looking at various methods to do this but it just doesn't seem to have any effect (as in, nothing happens - no errors, but the document doesn't update.
Probably easier just to show the code. First, here's an example of a document in this collection:
{
_id: 123,
//other fields go here
submissions: [
{
submission: 1,
downloaded: null, //This should be an ISODate when populated
assessor: null, //This should be an ObjectID when populated
feedbackUploaded: null, //This should also be an ISODate
path: '401Test1.doc',
assessorNotes: null //This should be a string when populated
},
{
submission: 2,
downloaded: null,
assessor: null,
feedbackUploaded: null,
path: '401Test2.doc',
assessorNotes: null
}
]
}
And now my mongoose query (within an express route):
const unit = req.body.unit; //e.g. 123
const submission = req.body.submission // e.g. 2
const date = Date.now();
Unit.update({ "_id": unit, "submissions.submission": submission }, {
$set: {
'submissions.assessorNotes': req.body.assessorComments,
'submissions.feedbackUploaded': date,
'submissions.assessor': req.body.assessor
}, function(err){
//callback
}
})
I've also tried this using $ notation in the $set command i.e. 'submissions.$.assessorNotes': req.body.assessorComments' but this doesn't seem to work either.
Where am I going wrong?!
Cheers!
Chris
Just tested this with the positional operator as mentioned above by TomG in a shell.
> db.foo.find().pretty()
{
"_id" : 123,
"submissions" : [
{
"submission" : 1,
"downloaded" : null,
"assessor" : null,
"feedbackUploaded" : null,
"path" : "401Test1.doc",
"assessorNotes" : null
},
{
"submission" : 2,
"downloaded" : null,
"assessor" : null,
"feedbackUploaded" : null,
"path" : "401Test2.doc",
"assessorNotes" : null
}
]
}
Then I ran the update command:
db.foo.update(
{ _id: 123, "submissions.submission": 2 },
{ $set: {
"submissions.$.assessorNotes": "testAssessorNotes",
"submissions.$.feedbackUploaded": new Date(),
"submissions.$.assessor": "testAssessor"
} }
)
Results are as follows:
{
"_id" : 123,
"submissions" : [
{
"submission" : 1,
"downloaded" : null,
"assessor" : null,
"feedbackUploaded" : null,
"path" : "401Test1.doc",
"assessorNotes" : null
},
{
"submission" : 2,
"downloaded" : null,
"assessor" : "testAssessor",
"feedbackUploaded" : ISODate("2016-10-26T15:33:09.573Z"),
"path" : "401Test2.doc",
"assessorNotes" : "testAssessorNotes"
}
]
}
Edit: My hunch is that the error could also be with the update in Mongoose, you could try changing both to the positional operators, as well as using findOneAndUpdate if it fits your needs instead of update.
Please try this query for Mongoose:
Unit.update(
{ "_id": unit, "submissions.submission": submission },
{ $set: {
'submissions.$.assessorNotes': req.body.assessorComments,
'submissions.$.feedbackUploaded': date,
'submissions.$.assessor': req.body.assessor
} }, function(err, result) {
//callback
}
});
I have collection "groups". like this:
{
"_id" : "e9sc7ogDp8pwY2uSX",
"groupName" : "one",
"creator" : "KPi9JwvEohKJsFyL4",
"eventDate" : "",
"isEvent" : true,
"eventStatus" : "Event announced",
"user" : [
{
"id" : "xfaAjgcSpSeGdmBuv",
"username" : "1#gmail.com",
"email" : "1#gmail.com",
"order" : [ ],
"price" : [ ],
"confirm" : false,
"complete" : false,
"emailText" : ""
},
...
],
...
"buyingStatus" : false,
"emailTextConfirmOrder" : " With love, your Pizzaday!! "
}
How can I get a value of specific element? For example i need to get value of "Groups.user.confirm" of specific group and specific user.
I tried to do so in methods.js
'pizzaDay.user.confirm': function(thisGroupeId, thisUser){
return Groups.find({ _id: thisGroupeId },{"user": ""},{"id": thisUser}).confirm
},
but it returns nothing.
Even in mongo console I can get just users array using
db.groups.findOne({ _id: "e9sc7ogDp8pwY2uSX"},{"user": ""})
The whole code is github
http://github.com/sysstas/pizzaday2
Try the following query:-
db.groups.aggregate(
[
{
$match:
{
_id: thisGroupeId,
"user.id": thisUser
}
},
{
$project:
{
groupName : 1,
//Add other fields of `user` level, if want to project those as well.
user:
{
"$setDifference":
[{
"$map":
{
"input": "$user",
"as": "o",
"in":
{
$eq : ["$$o.id" , thisUser] //Updated here
}
}
},[false]
]
}
}
}
]);
The above query will give the object(s) matching the query in $match inside user array. Now you can access any field you want of that particular object.
'pizzaDay.user.confirm': function(){
return Groups.findOne({ _id: thisGroupeId }).user.confirm;
I resolved it using this:
Template.Pizzaday.helpers({
confirm: function(){
let isConfirm = Groups.findOne(
{_id: Session.get("idgroupe")}).user.filter(
function(v){
return v.id === Meteor.userId();
})[0].confirm;
return isConfirm;
},
});
But I still think that there is some much elegant way to do that.