well... I don't know anything about not relational data base and I wanna know if the following script is correct.
The data base it's developed for obtain management of users and schedules, (The schedules are related with a professor and a classroom). The classrooms have a code and they can be commented and rated by users.
db.user.insert({
//Can be 6 o 10 digits
_id : 201309,
name : {
First : "Fernando",
Last : "Sarmiento"
},
area : 2,
account : {
email : "email#google.com",
password : "Password1!",
level : 0
},
status : {
status : "Active",
Date : 17-01-2016
},
//Can be null
comment : "I like cereal",
})
-
db.schedule.insert({
_id: ObjectId(),
day : "Lunes",
hour : 13 : 00,
finalHour : 14 : 00,
teacher : {
$ref : "user",
$id : $id.user
},
classroom : {
$ref : "classroom",
$id : $id.classrom
}
})
-
db.classroom.insert({
_id : ObjectId(),
name : "Software Development",
comment : "classrom 3",
//Can take values from 0 to 5
score : $avg(userScore),
code : [
{
code : "ABCDEF123456",
user : [{
$ref : "user",
$id : $id.user
}]
}
],
post : [
{
post : "I like the classrom :)",
date : 17-01-2016,
user : [{
$ref : "user",
$id : $id.user
}]
}
],
userScore : [{
score : 4.5,
user : [{
$ref : "user",
$id : $id.user
}]
}]
})
Thanks! :3
Related
I have updateMany function as follows
Article.updateMany({author: userId}, {author: anonym}, function(err, updated) {
if (err) {
res.send(err);
} else {
res.send(updated);
}
});
userId is = 6068b57dbe4eef0b579120c7
anonym is = 6069870676d6320f39e7e5a2
for testing purposes I have a single article in MongoDB as follows
db.articles.find()
{ "_id" : ObjectId("6068b591be4eef0b579120c8"), "favoritesCount" : 1, "comments" : [ ], "tagList" : [ ], "title" : "Martin", "description" : "Testib", "body" : "Asju", "author" : ObjectId("6068b57dbe4eef0b579120c7"), "slug" : "martin-2hzx78", "createdAt" : ISODate("2021-04-03T18:36:01.977Z"), "updatedAt" : ISODate("2021-04-03T18:53:29.809Z"), "__v" : 0 }
You can see that article has "author" : id field in it which currently shows userId as author.
I want to update that field and transfer authorship to anonym user.
When I send this request to postman I get following response
{
"n": 0,
"nModified": 0,
"ok": 1
}
And database remains unchanged. What am I doing wrong here ?
I have documents in which I would like to update the hostUser with one of the members of the document,also have to delete the record from the member document and add the chips of the deleted member in the club chips.
Here is the sample document.
{
"_id" : "1002",
"hostUser" : "1111111111",
"clubChips" : 10000,
"requests" : {},
"profile" : {
"clubname" : "AAAAA",
"image" : "0"
},
"tables" : [
"SJCA3S0Wm"
],
"isDeleted" : false,
"members" : {
"1111111111" : {
"chips" : 0,
"id" : "1111111111"
},
"2222222222" : {
"chips" : 0,
"id" : "2222222222"
}
}
}
This is what I have tried.
db.getCollection('test').updateMany({"hostUser":"1111111111"},
{"$set":{"hostUser":"2222222222"},"$unset":{"members.1111111111":""}})
This is how you would handle unset and set in a single call to updateMany. Can you please clarify what you meant by "check if the values exist in the member field"?
db.getCollection('test').updateMany(
{"hostUser":"1111111111"},
{
'$set': {"hostUser":"2222222222"} ,
'$unset': {"members.1111111111":""}
}
)
I have two collections one is websites which stores information like:
{
"_id" : ObjectId("5ac5efd6a37efa4c0e28f5aa"),
"main_id" : 3,
"status" : "",
"website" : "http://test.com",
"last_access_time" : "2018-04-16 17:49:03",
"links" : [
{
"link_id" : "test-1",
"link" : "test1.html"
},
{
"link_id" : "test-2",
"link" : "test.html"
}
]
}
And another is website_info in which I want store info like:
{
"_id" : ObjectId("5ad72ddecf45b60dffcbf9f2"),
"main_id" : 3,
"last_access_time" : "2018-04-18 15:37:02",
"test-1" : {
"no_of_links" : 55,
"links_2" : [
{
"link" : "/home",
"link_id" : "secnd-1",
},
{
"link" : "/login",
"link_id" : "secnd-2",
},
{
"link" : "/services",
"link_id" : "secnd-3",
}
]
},
"test-2" : {
"no_of_links" : 55,
"links_2" : [
{
"link" : "/home",
"link_id" : "secnd-1",
},
{
"link" : "/login",
"link_id" : "secnd-2",
},
{
"link" : "/services",
"link_id" : "secnd-3",
}
]
}
}
I am using Python3 and mongoDB.
Here I want to check the field like "link_id" which is "test-1" in the website_info for main_id = 3 exists or not. If it is exists I will update for same, if does not exists I want to insert new record set.
The thing is how to check whether field "test-1" (which is the value from websites collection) in website_info collection exists or not.
Help is appreciated.
Here in my case, link_id will be unique in website_info collection. So no need to check for main_id, only checking for link_id solved my issue, like:
#classmethod
def find_link(self, link_id):
cursor = self.db.collection.find({link_id: {'$exists': True}} )
results = list(cursor)
return results
And check for exists like:
if(len(is_exists)>0):
#do if exists
I am stuck here to write mongoose schema for happenings and birthdays fields. I am using mongodb/mongoose and would like to save the similar contents in events collections.
{
"_id" : ObjectId("5938fc171dfe0f225902d85d"),
"month" : 6,
"date" : 9,
"happenings" : [
{
"incident" : "Muhammad, the founder of Islam and unifier of Arabia, died.",
"year" : "632"
},
{
"incident" : "The Army of the Potomac defeats Confederate forces at Battle of Cross Keys, Virginia..",
"year" : "1862"
},
{
"incident" : "Israeli airplanes attack the USS Liberty, a surveillance ship, in the Mediterranean, killing 34 Navy crewmen..",
"year" : "1967"
},
{
"incident" : "Gemini astronaut Gene Cernan attempts to become the first man to orbit the Earth untethered to a space capsule, but is unable to when he exhausts himself fitting into his rocket pack.",
"year" : "1966"
}
],
"birthdays" : {
"actor" : {
"name" : "Josh Pence",
"yob" : 1982,
"birthplace" : "Santa Monica, CA",
"role" : "MOVIE ACTOR",
"image" : "josh_1982.png"
},
"actress" : {
"name" : "Julianna Margulies",
"yob" : 1966,
"birthplace" : "Spring Valley, NY",
"role" : "TV ACTRESS",
"image" : "julianna_1966.png"
},
"player" : {
"name" : "Julianna Margulies",
"yob" : 1987,
"birthplace" : "Ohio",
"role" : "FOOT BALL",
"image" : "julianna_1966.png"
}
}
}
The schema I tried
var schema = new Schema({
day: Number,
Month: Number,
birthdays: Schema.Types.Mixed,
happenings: [],
incident: [String],
year: [Number],
})
var event= mongoose.model('Event', schema);
How should I modify the above schema??
According to your document I think your schema could be like
var schema = new Schema({
day: Number,
Month: Number,
birthdays: Schema.Types.Mixed,
happenings: [{
incident: String,
year: String,
_id: false
}]
});
var event= mongoose.model('Event', schema);
I have a MongoDB query based in Objects as "Ticket" structure, each ticket usually contains Tasks Object and each of them have an Owner, if the Ticket is Open, the associate Owner is “OpnPrps.CurAgtNme”, by other hand if the ticket is Closed, the associate Owner is “Nms.CloAgt”.
This is how my database (JSON) look like:
{
"result" : [
{
"_id" : NumberLong(3131032306336),
"TicId" : 1147552,
"OrgId" : 729,
"Sts" : "Closed",
"CrtDat" : ISODate("2015-04-23T18:50:46.000Z"),
"CloDat" : ISODate("2015-04-23T19:46:26.000Z"),
"ShtDes" : "Copy of Employment agreement",
"Des" : "EE wants a copy of employment agreement. address was verified.",
"DesSum" : "EE wants a copy of employment agreement. address was verified.",
"Sol" : "ISIGHT TICKET NUMBER:<br><h1>US-04-15-01109</h1>",
"CrtAgtId" : 20444,
"CloAgtId" : 20149,
"CrtApp" : null,
"IsInt" : false,
"HasPrtTsk" : null,
"PexBodId" : "",
"PayGrp" : "",
"RvwDat" : null,
"RclDat" : null,
"IsDynDueDatDef" : false,
"OlaDat" : ISODate("2015-04-25T00:50:50.000Z"),
"SlaDat" : ISODate("2015-04-25T00:50:50.000Z"),
"DynDueDatElaDat" : null,
"ReaTim" : "00:00:00",
"LstUpd" : ISODate("2015-04-23T19:46:26.000Z"),
"VrsOnl" : 2,
"VrsArc" : 1,
"OpnPrps" : null,
"Nms" : {
"Cmp" : "Organization1 US",
"Org" : "Organization1",
"Srv" : "Policies",
"SrvGrp" : "17. Workforce Administration (NGR)",
"Wkg" : "WORKGROUP1",
"Pri" : "",
"Tir" : "T1",
"Src" : "Call",
"CrtAgt" : "Arun ",
"CloAgt" : "Felicia"
},
"Olas" : {
"_id" : 2,
"EntNam" : "ENTITY",
"DueDat" : ISODate("2015-04-25T00:50:50.000Z"),
"AmbDat" : ISODate("2015-04-24T20:50:49.000Z"),
"DueDatElaDat" : null,
"AmbDatElaDat" : null,
"SlaDuration" : 18,
"TotTim" : NumberLong(0),
"TotTimPndEnt" : NumberLong(0),
"TotTimPndEmp" : NumberLong(0),
"RclInSla" : false
},
"Tsks" : {
"_id" : 1,
"Typ" : "Planned",
"CrtDat" : ISODate("2015-04-23T18:50:46.000Z"),
"CloDat" : null,
"LstUpd" : ISODate("2015-04-23T18:50:46.000Z"),
"DueDat" : ISODate("2015-04-25T00:50:50.000Z"),
"TimCplTsk" : 1080,
"DueDatEla" : false,
"AgtOwnId" : null,
"WkgSklId" : 45387,
"EntId" : 2,
"Sts" : "Open",
"PrdTskId" : 201,
"Ttl" : "Provide Navigational Assistance",
"Des" : "Provide Navigational Assistance",
"SrvSklId" : 45792,
"PriSklId" : null,
"TotTim" : 0,
"PtnId" : null,
"PtnTic" : null,
"DepTskId" : null,
"IsAct" : true,
"IsMndOnCloTic" : false,
"Nms" : {
"AgtOwn" : null,
"Wkg" : "CM_T1",
"Ent" : "ENTITY",
"SrvSkl" : "Policies",
"PriSkl" : null,
"Ptn" : null,
"Frm" : ""
},
"AscTicItm" : null,
"FrmId" : null,
"Flds" : []
},
And the query I'm using to group the data looks like:
db.tickets.aggregate([
{$match:{
'Nms.Org': 'Organization1',
'Nms.Cmp':'Company',
'Nms.Wkg':'Workgroup’
}},
{$project:{
_id:0,
'Tsks._id':1,
'Tsks.Sts':1,
'Tsks.DueDat':1,
'Sts':1,
'Nms.Org':1,
'Nms.Cmp':1,
'Nms.Wkg':1,
'Nms.CloAgt':1,
'OpnPrps.CurAgtNme':1,
'OpnPrps.CurEntNme':1,
'Olas.EntNam':1
}},
{$unwind : "$Olas" },
{$unwind : "$Tsks" },
{$match:{$and:[{
'Tsks.DueDat': {$ne: null},
'Olas.EntNam': 'Entity',
'Tsks._id':{$gt:0}
}]}},
{$group:
{_id:{
Org:'$Nms.Org',
Cmp:'$Nms.Cmp',
Wkg:'$Nms.Wkg',
CurEntNme:'$Olas.EntNam',
CurTskId: '$Tsks._id',
DueDate:'$Tsks.DueDat',
Owner1:'$OpnPrps.CurAgtNme',
Owner2:'$Nms.CloAgt'
},
All: {$sum: { $cond:
[
{ $eq: [ '$Tsks.DueDat' , null ] } ,0,1
]}}
},
}
])
With this query I’m able to get every ticket for every owner (even if they’re open or closed) and their associate tasks, my problem is that I want to group all the tickets without taking in consideration the status, something like a second group after the results I have now, see below:
The table I get with Jasper Studio with this query looks like:
Owner Inventory
--------- --------------
Noemi 1 Owner1:Noemi | Owner2:null
Carl 2 Owner1:null | Owner2:Carl
Darla 2 Owner1:Darla| Owner2:null
Carl 1 Owner1:Carl| Owner2:null
Paola 2 Owner1:null| Owner2:Paola
Noemi 2 Owner1:null | Owner2:Noemi
As you can see, I’m getting repeated values due to the different field from each ticket. The table I'm looking for should be like this:
Owner Inventory
--------- --------------
Noemi 3 Owner1:Noemi | Owner2:Noemi
Carl 3 Owner1:Carl | Owner2:Carl
Darla 2 Owner1:Darla| Owner2:null
Paola 2 Owner1:null| Owner2:Paola
So, my problem is that I can’t find the way to group these results again to obtain the second table.