I'm trying to update the comment_delete = 'false' to 'true' on the second object within an array. Help Please ....
"_id" : "jLkRdxocZzheefWF3",
"comments" : [
{
"comment_id" : "\u0003624334",
"comment" : " test",
"user" : "peter pan",
"userId" : "MQtp4i8bZeLYSLbr5",
"comment_delete" : "false"
},
{
"comment_id" : "\u0007973101",
"comment" : " add",
"user" : "peter pan",
"userId" : "MQtp4i8bZeLYSLbr5",
"comment_delete" : "false"
}
],
}
Try this query:
db.collection.update(
{_id:"jLkRdxocZzheefWF3"}, //add your first match criteria here, keep '{}' if no filter needed.
{$set:{"comments.$[element].comment_delete":"true"}},
{arrayFilters:[{"element.comment_id":"\u0007973101"}]}
)
I dont have an idea about your match criteria since you didnt mention it in the question. Change them as per your requirements. This change comment_delete to true as per the comment_id mentioned.
Output is:
{
"_id" : "jLkRdxocZzheefWF3",
"comments" : [
{
"comment_id" : "\u0003624334",
"comment" : " test",
"user" : "peter pan",
"userId" : "MQtp4i8bZeLYSLbr5",
"comment_delete" : "false"
},
{
"comment_id" : "\u0007973101",
"comment" : " add",
"user" : "peter pan",
"userId" : "MQtp4i8bZeLYSLbr5",
"comment_delete" : "true"
}
]
}
db.users.update({'_id':'jLkRdxocZzheefWF3',"comments.comment_id":"\u0007973101"},{$set:{'comments.$.comment_delete':true}})
Try above mentioned query it works
Related
I have a mongoose document that is a User. Each user has arrays or words e.g. nouns. I want to be able to update the fields of the words e.g. add a +1 to the frequency or change the status number. I can access the whole array but not the individual items in the array. Also i want to be able to display the items in the array based on their fields e.g. only display those with frequency of 3 or more etc...
I have tried lots of different things but I'm just not getting it. I keep getting back error messages that findOneAndUpdate is not a function, or 'undefined' when I try to access and update documents.
{
"_id" : ObjectId("5f7d8490c1842471c6bcea42"),
"username" : "eric#mail.com",
"nouns" : [
{
"_id" : ObjectId("5f7d849cc1842471c6bcea43"),
"word" : "die Sonne",
"timeStamp" : ISODate("2020-10-07T09:04:28.436Z"),
"frequency" : 0,
"status" : 0
},
{
"_id" : ObjectId("5f7d84a8c1842471c6bcea45"),
"word" : "das Wetter",
"timeStamp" : ISODate("2020-10-07T09:04:40.940Z"),
"frequency" : 0,
"status" : 0
},
{
"_id" : ObjectId("5f7d84afc1842471c6bcea47"),
"word" : "der Apfel",
"timeStamp" : ISODate("2020-10-07T09:04:47.403Z"),
"frequency" : 0,
"status" : 0
},
{
"_id" : ObjectId("5f7d84b9c1842471c6bcea49"),
"word" : "das Maedchen",
"timeStamp" : ISODate("2020-10-07T09:04:57.388Z"),
"frequency" : 0,
"status" : 0
},
{
"_id" : ObjectId("5f7d84c8c1842471c6bcea4b"),
"word" : "das Auto",
"timeStamp" : ISODate("2020-10-07T09:05:12.036Z"),
"frequency" : 0,
"status" : 0
}
],
"verbs" : [],
"adjectives" : [],
"others" : [],
"salt" : "*****",
"hash" : "*****",
"__v" : 5
}
figured it out after hours
const ObjectId = require('mongodb').objectID
db.collections.users.updateOne(
{},
{$set: {"nouns.$[element].status": 3}},
{multi: true,
arrayFilters: [{"element._id":
ObjectId(req.body.inputId)}]}
);
I am trying to read some key-value from one object using Javascript but one key value is not reading at all. I am explaining my code below.
customer={
"_id" : ObjectId("5e21bdb921a1de5a8885c368"),
"IsActive" : true,
"GroupName" : "ashok",
"CustomerId" : "7008980097Saikantassss#okedqart",
"GroupCode" : "ashokgrp",
"GroupType" : "Group",
"GroupDescription" : "test description",
"GroupImage" : "",
"StoreCode" : "DKWF",
"GroupID" : "1673e78b-8d35-4c5b-9bda-35d8788a64ad",
"CreatedAt" : ISODate("2020-01-17T13:59:21.373Z"),
"UpdatedAt" : ISODate("2020-01-17T13:59:21.373Z"),
"__v" : 0
}
Here I have the above fetched value from mongodb but when I am printing the customerId value like below.
console.log(customer['CustomerId']);
//undefined
The above value is coming undefined but same time console.log(customer['GroupCode']); giving the value as ashokgrp. Here I need to print also customer['CustomerId'] value.
var customer={
"_id" : "5e21bdb921a1de5a8885c368",
"IsActive" : true,
"GroupName" : "ashok",
"CustomerId" : "7008980097Saikantassss#okedqart",
"GroupCode" : "ashokgrp",
"GroupType" : "Group",
"GroupDescription" : "test description",
"GroupImage" : "",
"StoreCode" : "DKWF",
"GroupID" : "1673e78b-8d35-4c5b-9bda-35d8788a64ad",
"CreatedAt" : "2020-01-17T13:59:21.373Z",
"UpdatedAt" : "2020-01-17T13:59:21.373Z",
"__v" : 0
}
console.log(customer.CustomerId);
It Will work .
I am trying to filter firebase data using startAt and/or endAt.
My data is structured as below.
{
"notes" : {
"-LOs0Ikx4ydM5RatREM1" : {
"data" : {
"dueDate" : 1561629600000,
"description" : "Korewa nan desuka?!",
"createdAt" : 1539611900000,
"title" : "First "
},
"members" : {
"author" : "1212121212121212121212121212"
}
},
"-LOs0Ikx4ydM5RatREM2" : {
"data" : {
"dueDate" : 4004870448000,
"description": "Test"
"createdAt" : 1539611900000,
"title" : "Second"
},
"members" : {
"author" : "1212121212121212121212121212"
}
},
"-LhBt9msLFKqUQ-koI9W" : {
"data" : {
"dueDate" : 1564653600000,
"description" : "abc",
"createdAt" : 1560363158279,
"title" : "August 1"
},
"members" : {
"author" : "3434343434343434343434343434"
}
},
"-LhBtKdDrQv9eKuYdfCi" : {
"data" : {
"dueDate" : 1564653600000,
"description" : "abcdef",
"createdAt" : 1560363158279,
"title" : "August 2"
},
"members" : {
"author" : "3434343434343434343434343434"
}
}
}
}
What I wish is to fetch all "notes" where dueDate has passed.
const now = moment().valueOf() //Eg. 1561629500000
database.ref('notes/')
.orderByChild("dueDate")
.endAt(now)
.once("value", (snapshot) => {
console.log('Process expired notes')
snapshot.forEach( (data) => {
const obj = data.val()
console.log('Date comparison:', (now >= obj.data.alertDate))
...
The code above does not work, it returns all the objects from the example JSON. The console.log logs "False" for three out of four returned objects.
I could do a comparison and only process the objects that meets my criteria, but that would defeat the purpose.
I have indexed the database on ["notes\data\alertDate"].
What am I missing? I must have misinterpreted the documentation somehow. :)
Your dueDate property is nested under data, so you need to address is as data/dueDate:
database.ref('notes')
.orderByChild("data/dueDate")
You might want to include both a startAt() and endAt() clause, with just a reasonable value for startAt() and the specific value you're already using for endAt().
I have a mongo document like so:
{
"_id" : "EimL8Hf5SsCQKM7WP",
"enable" : "no",
"collector" : [
{
"name" : "prod",
"enable" : "no",
"transport" : {
"address" : {},
"port" : "8000",
"protocol" : "http-secure"
},
"authentication" : {},
"notify-syslog" : "yes"
},
{
"name" : "test",
"enable" : "no",
"transport" : {
"address" : {},
"port" : "8000",
"protocol" : "http-secure"
},
"authentication" : {},
"notify-syslog" : "yes"
}
],
"misc" : {
"createdBy" : "someuser",
"updatedBy" : "someuser",
}
}
I display certain fields of the mongo document as an autoform to the user so that they can be edited. Since the other fields are hidden, the outcome of the form will only contain the fields that were exposed.
I end up with a modified document object like so: (keep an eye on the "name" fields)
{
"enable" : "no",
"collector" : [
{
"name" : "someohername",
},
{
"name" : "anothername",
}
],
"misc" : {
"createdBy" : "someuser",
"updatedBy" : "anotheruser",
}
}
My current requirement is to somehow merge both the documents together such that the initial document has all fields of the modified document, along with it's fields.
Outcome:
{
"_id" : "EimL8Hf5SsCQKM7WP",
"enable" : "no",
"collector" : [
{
"name" : "someothername",
"enable" : "no",
"transport" : {
"address" : {},
"port" : "8000",
"protocol" : "http-secure"
},
"authentication" : {},
"notify-syslog" : "yes"
},
{
"name" : "anothername",
"enable" : "no",
"transport" : {
"address" : {},
"port" : "8000",
"protocol" : "http-secure"
},
"authentication" : {},
"notify-syslog" : "yes"
}
],
"misc" : {
"createdBy" : "someuser",
"updatedBy" : "anotheruser",
}
}
If you notice above, only the name field has been updated whilst retaining the remaining fields.
I tried to directly update the document using collection.update({id}, $set:{<<modified_docuemnt}}); but it is replacing the entire collector field with what is passed in the $set
How exactly do I go about this? It does not matter if I'm doing the changes directly in mongodb or as a JSON object using javascript.
My current doc is like this
{
"_id" : ObjectId("55ece69df332eb0000d34e12"),
"parent" : "P1",
"hierarchy" : {},
"hidden" : false,
"type" : "xyz",
"name" : "Mike",
"code" : "M110",
"date" : ISODate("2015-09-07T01:21:33.965Z"),
"__v" : 3,
"job_id" : "ca50fdf0-6904-11e6-b9af-1b0ea5d7f792"
}
now i want to have job_id as array instead. So i changed the field type to array in model schema. Now when I try to update the existing docs with some changes.
Document before saving looks like this
{
"_id" : ObjectId("55ece69df332eb0000d34e12"),
"parent" : "P1",
"hierarchy" : {},
"hidden" : false,
"type" : "xyz",
"name" : "Mike",
"code" : "M110",
"date" : ISODate("2015-09-07T01:21:33.965Z"),
"__v" : 3,
"job_id" : ["ca50fdf0-6904-11e6-b9af-1b0ea5d7f792",
"f1f04a95-42fa-41e5-a2c6-89c52c9c63f2"
]
}
so when i call model.save() method i'm getting following error:
{ [MongoError: The field 'job_id' must be an array but is of type String in document {_id: ObjectId('55ece69df332eb0000d34e12')}]
name: 'MongoError',
code: 16837,
err: 'The field \'job_id\' must be an array but is of type String in document {_id: ObjectId(\'55ece69df332eb0000d34e12\')}' }
What is the best way to handle this??