I have a mongo json object as follows
{
"_id" : new BinData(3, "RDHABb22XESWvP83FplqJw=="),
"name" : "NEW NODE",
"host" : null,
"aet" : null,
"studies" : ["1.3.12.2.1107.5.99.3.30000008061114424970500000589"],
"testcases" : [new BinData(3, "Zhl+zIXomkqAd8NIkRiTjQ==")],
"sendentries" : [{
"_id" : "1.3.12.2.1107.5.99.3.30000008061114424970500000589",
"Index" : 0,
"Type" : "Study"
}, {
"_id" : "cc7e1966-e885-4a9a-8077-c3489118938d",
"Index" : 1,
"Type" : "TestCase"
}]
}
The fields "Studies" and "TestCases" are now obsolete and I am now storing that information in a new field called SendEntries. I would like to get rid of the Studies and TestCases from the old entries and unmap those fields going forward. I want to know how I can update my current collections to get rid of the Studies and TestCases fields.
I'm just few weeks into Mongo.
You can use the $unset operator with update.
db.collection.update({},
{ $unset: {
"studies": "",
"testcases": ""
},
{ "upsert": false, "muti": true }
)
And that will remove all of the fields from all of your documents in your collection
Use $unset, there's a manual page e.g.
db.yourCollection.update( { },
{ $unset: {
Studies: "",
testcases: ""
}
},
{ multi: true }
)
Related
I try to build a json for me to test my firebase application. I need a couple of hundred entries and for that reason try to build me json with the JSON Generator from https://www.json-generator.com/
How ever I have problems to get random IDs as keys (and not as values). I tried all I can think of with {{objectId()}} but this seem to work only for values.
Does anybody have an idea how I could build a RANDOM JSON like the example below with the JSON Generator I used or any other generator? Again the problem is the random KEYS! As values this works great.
here an example of the JSON i need at the end.
{
"-f2zawhcNgh6vcHIQOCq" : {
"cat_id" : 1,
"created_at" : 1516206514529,
"edited_at" : 1516206514529,
"groups" : {
"-L2zawhcNgh6vcHIQOCq" : true,
"-L2zaxyBs-9AfQJii47k" : true
},
"local" : true,
"owner_id" : "MyaNhS4Qy6foMYkSIt0tpRQFneI3",
"private" : false
},
"-fghgajhgfkjhkQOCq" : {
"cat_id" : 1,
"created_at" : 1516206514529,
"edited_at" : 1516206514529,
"groups" : {
"-L2zawkjhfkjhkjgQOCq" : true
},
"local" : true,
"owner_id" : "hgfjhYkSIt0tpRQFneI3",
"private" : false
}
}
JSON.stringify(new Array(200).fill('something').reduce(mock => {
mock[Math.random()] = {
"cat_id" : 1,
"created_at" : Date.now()*Math.random() | 0,
"edited_at" : Date.now(),
"groups" : {
[Math.random()] : true
},
"local" : true,
"owner_id" : Math.random(),
"private" : false
}
return mock
}, {}), undefined, 2)
Paste this in chrome js console or node.js
Is that what you are looking for?
The title of the question is self explanatory. I want to know what differences are there in JSON Document A which comes from API request and JSON Document B which is already in Mongo DB.how to get changes column name and data also.. i am creating log..that's why i want...
Below is the code of what I'm trying:
NodeJS APICode//
function Updatejob(req, res) {
return function (jobSchedule) {
var obj = new Date();
CompareJSON(req, mongodbjson);
return Job.create(req.body).then(.....)
}
Already Data in Mongodb before Update Record
{
"_id" : ObjectId("586d1032aef194155028e9c7"),
"history" : [
{
"_id" : ObjectId("586d1032aef194155028e9c4"),
"updated_by" : "",
"details" : "Job Created",
"changetype" : "Created",
"datetime" : ISODate("2017-01-04T15:09:38.465Z")
}
],
"current_status" : "Pending",
"time" : 0
}
//REQUEST FOR UPDATE DATA
{
"_id" : ObjectId("586d1032aef194155028e9c7"),
"history" : [
{
"_id" : ObjectId("586d1032aef194155028e9c4"),
"updated_by" : "",
"details" : "Job Completed",
"changetype" : "Completed",
"datetime" : ISODate("2017-01-04T15:09:38.465Z")
}
],
"current_status" : "Completed",
"time" : 0
}
You can use jsondiffpatch:
var delta = jsondiffpatch.diff(object1, object2);
See:
https://www.npmjs.com/package/jsondiffpatch
I have this JSON structure:
{
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : {
"author" : "rsenov",
"members" : {
"-KBxo7ZU6McsmDOxyias" : true,
"-KBxo8_TUTW6NZze6xcd" : true,
"rsenov" : true
},
"name" : "Prueba 3"
}
},
"users" : {
"-KBxo7ZU6McsmDOxyias" : {
"avatar" : "owl2",
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : true
},
"isUser" : false,
"name" : "Pepa"
},
"-KBxo8_TUTW6NZze6xcd" : {
"avatar" : "monkey",
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : true
},
"isUser" : false,
"name" : "Lolas"
},
"rsenov" : {
"avatar" : "guest",
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : true
},
"isUser" : true,
"name" : "Ruben",
}
}
}
and the security&rules file is:
{
"rules": {
".read": true,
".write": true,
"users": {
".indexOn": ["email", "groups"]
},
"groups": {
".indexOn": ["author", "name"]
}
}
}
I'm trying to run a query in order to get the ChildChanged snapshot:
DataService.dataService.USERS_REF.queryOrderedByChild("groups").queryEqualToValue(currentGroup.groupKey).observeEventType(.ChildChanged, withBlock: {snapshot in
print(snapshot)
})
DataService.dataService.USERS_REFcorresponds to the url that point to the "users" key, and currentGroup.groupKeyis equal to -KBxo9-RoY0eowWKeHkUin this case.
According to this query, I should get the snapshot of the child that has changed. For example, if I replace the user name "Pepa" to "Test", I should get the snapshot:
"-KBxo7ZU6McsmDOxyias" : {
"avatar" : "owl2",
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : true
},
"isUser" : false,
"name" : "Test"
}
but this query never get's called...
Is there something wrong in my query?
"I'm trying to run a query in order to get the ChildChanged snapshot:" is a little odd.
You can query for data, or observe a node via ChildChanged.
If you just want to be notified of changes within the users node, add an observer to that node and when Pepa changes to Test, your app will be notified and provided a snapshot of the user node that changed.
var ref = Firebase(DataService.dataService.USERS_REF)
ref.observeEventType(.ChildChanged, withBlock: { snapshot in
println("the changed user is: \(snapshot.value)")
})
Oh, and no need for queryOrderedByChild since the snapshot will only contain the single node that changed.
I want update a array value that is nested within an array value: i.e. set
status = enabled
where alerts.id = 2
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"locations" : [
{
"name": "my location",
"alerts" : [
{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
]
}
I would have used the position $ character, but cannot use it twice in a statement - multi positional operators are not supported yet: https://jira.mongodb.org/browse/SERVER-831
How do I issue a statement to only update the status field of an alert matching an id of 2?
UPDATE
If I change the schema as follows:
{
"_id" : ObjectId("5496ab2149847b6cd7c7b352"),
"name" : "joe",
"locations" : {
"my location" : {
"alerts" : [
{
"id" : 1,
"status" : "enabled"
},
{
"id" : 2,
"status" : "enabled"
}
]
},
"my other location" : {
"alerts" : [
{
"id" : 3,
"status" : null
},
{
"id" : 4,
"status" : null
}
]
}
}
}
I can then use:
update({"locations.my location.alerts.id":1},{$set: {"locations.my location.alerts.$.status": "enabled"}});
Problem is I cannot create indexes on the alert id :-(
it may be better of modelled as such, specially if an index on location and,or alerts.id is needed.
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"location" : "myLocation",
"alerts" : [{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"location" : "otherLocation",
"alerts" : [{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
I think you are having a wrong tool for the job. What you have in your example is relational data and it's much easier to handle with relational database. So I would suggest to use SQL-database instead of mongo.
But if you really want to do it with mongo, then I guess the only option is to fetch the document and modify it and put it back.
I have been using the following MySQL command to construct a heatmap from log data. However, I have a new data set that is stored in a Mongo database and I need to run the same command.
select concat(a.packages '&' b.packages) "Concurrent Packages",
count(*) "Count"
from data a
cross join data b
where a.packages<b.packages and a.jobID=b.jobID
group by a.packages, b.packages
order by a.packages, b.packages;
Keep in mind that the tables a and b do not exist prior to the query. However, they are created from the packages column of the data table, which has jobID as the field which I want to check for matches. In other words if two packages are within the same job I want to add an entry to the concurrent usage count. How can I generate a similar query in Mongo?
This is not a "join" of different documents; it is an operation within one document, and can be done in MongoDB.
You have a SQL TABLE "data" like this:
JobID TEXT,
package TEXT
The best way to store this in MongoDB will be a collection called "data", containing one document per JobID that contains an array of packages:
{
_id: <JobID>,
packages: [
"packageA",
"packageB",
....
]
}
[ Note: you could also implement your data table as only one document in MongoDB, containing an array of jobs which contain each an array of packages. This is not recommended, because you might hit the 16MB document size limit and nested arrays are not (yet) well supported by different queries - if you want to use the data for other purposes as well ]
Now, how to get a result like this ?
{ pair: [ "packageA", "packageB" ], count: 20 },
{ pair: [ "packageA", "packageC" ], count: 11 },
...
As there is no built-in "cross join" of two arrays in MongoDB, you'll have to program it out in the map function of a mapReduce(), emitting each pair of packages as a key:
mapf = function () {
that = this;
this.packages.forEach( function( p1 ) {
that.packages.forEach( function( p2 ) {
if ( p1 < p2 ) {
key = { "pair": [ p1, p2 ] };
emit( key, 1 );
};
});
});
};
[ Note: this could be optimized, if the packages arrays were sorted ]
The reduce function is nothing more than summing up the counters for each key:
reducef = function( key, values ) {
count = 0;
values.forEach( function( value ) { count += value } );
return count;
};
So, for this example collection:
> db.data.find()
{ "_id" : "Job01", "packages" : [ "pA", "pB", "pC" ] }
{ "_id" : "Job02", "packages" : [ "pA", "pC" ] }
{ "_id" : "Job03", "packages" : [ "pA", "pB", "pD", "pE" ] }
we get the following result:
> db.data.mapReduce(
... mapf,
... reducef,
... { out: 'pairs' }
... );
{
"result" : "pairs",
"timeMillis" : 443,
"counts" : {
"input" : 3,
"emit" : 10,
"reduce" : 2,
"output" : 8
},
"ok" : 1,
}
> db.pairs.find()
{ "_id" : { "pair" : [ "pA", "pB" ] }, "value" : 2 }
{ "_id" : { "pair" : [ "pA", "pC" ] }, "value" : 2 }
{ "_id" : { "pair" : [ "pA", "pD" ] }, "value" : 1 }
{ "_id" : { "pair" : [ "pA", "pE" ] }, "value" : 1 }
{ "_id" : { "pair" : [ "pB", "pC" ] }, "value" : 1 }
{ "_id" : { "pair" : [ "pB", "pD" ] }, "value" : 1 }
{ "_id" : { "pair" : [ "pB", "pE" ] }, "value" : 1 }
{ "_id" : { "pair" : [ "pD", "pE" ] }, "value" : 1 }
For more information on mapReduce consult: http://docs.mongodb.org/manual/reference/method/db.collection.mapReduce/ and http://docs.mongodb.org/manual/applications/map-reduce/
You can't. Mongo doesn't do joins. Switching from SQL to Mongo is a lot more involved than migrating your queries.
Typically, you would include all the pertinent information in the same record (rather than normalize the information and select it with a join). Denormalize!