I have two schemas:
Department:
{dept_id :Number,
name: String,
Member: [type: Schema.Types.ObjectId, ref :'Professor']
Professor:
{
deptid :{type:Number, ref:Department},
name: String,
age:Number}
I need to find a joined table consisting of employee details along with Department details.
How can i do that?
Related
I am very new to node.js and mongoose so bare with me if my question isn't using the right terms. I'm amazed I've gotten this far so quickly!
My main schema is a grocery list, which can have a listItemDict object, and an array of sale objects. I can PUT any field from the main schema and it will update just that field - the other fields stay as they were. But if I PUT any field from the sale schema, it replaces any sale data that was there.
My schema's are defined like this
var listItemDictSchema = mongoose.Schema(
{
name: String,
details: String,
aisle: String,
aisleSort: Number
}
);
var saleSchema = mongoose.Schema(
{
store: String,
price: Number,
quantity: Number,
quantityUnit: String,
details: String,
startDate: Date,
endDate: Date
}
)
var schema = mongoose.Schema(
{
name: String,
details: String,
quantity: Number,
quantityUnit: String,
priority: String,
forDate: Date
listItemDict: listItemDictSchema,
sales: [saleSchema]
},
{ timestamps: true }
);
This is a sample mongodb entry
_id:621ca04410e73258802a619d
name:"Bread"
details:""
quantity:1
quantityUnit:"loaf"
priority:"Normal"
listItemDict:Object
sales:
Array:
0:Object
_id:621ca66f10e73258802a61a5
store:"Store 1"
details:"BOGO"
price:1.99
1:
Object
_id:621ca66f10e73258802a61a6
store:"Store 2"
price:2.2
createdAt:2022-02-28T10:13:24.312+00:00
updatedAt:2022-02-28T10:39:43.768+00:00
If I put
{
"quantity": 2
}
just the quantity is updated and everything else stays the same, which I think is the right behaviour.
If I put
"sales": [{
"id": "621ca66f10e73258802a61a5",
"store": "Store 3"
}]
it replaces all sale info with this.
How can I update one piece of a sale object without replacing all sale data?
If you want to update the quantity of specific sales then pick sale document first by id and then update the fields you want in $set:
SalesModel.findByIdAndUpdate('id',{
$set:{
quantity:10
}
})
I have the following scheme:
var user = Schema({
id: Number,
name: String,
surname: String,
role: { type: Schema.Types.ObjectId, ref: "" }//member or crew
property: Number
});
var member = Schema({
cod_id: Number,
aa: String,
bb: String,
});
var crew = Schema({
cod_id: Number,
cc: String,
dd: String,
});
Member and crew, they are both users but they have different attributes.
The only attributes that are equal are: name, surname, role and property.
What I would like to understand if it were possible to do such a thing, specifying in user the role attribute that can be either member or crew, refer to the specific model in question.
Everything stems from the need to have the property attribute in a single model and not having to put this attribute in either member or crew, otherwise when I have to do a search I have to do two, one in the model member and one in the crew, waiting for don't have duplicate problems.
Can you give me some advice?
I have just started learning Node and MongoDB and I'm building an application for my wedding to try out these technologies.
Part of the application will focus on the assignment of guests to tables for the wedding breakfast. So far I have a Guest schema like so:
const guestSchema = new mongoose.Schema({
firstname: {
type: String,
trim: true,
},
surname: {
type: String,
trim: true,
},
attending: String,
allergies: String,
menu: String
});
const tableSchema = new mongoose.Schema({
name: String,
capacity: Number,
**guests: [
{ type: mongoose.Schema.ObjectId, ref: 'Guest' }
]**
I am aware that I can create a relationship between guests and tables as shown in bold but I'd like to limit the number of 'related' guests to a table according to the capacity of the table. E.g - tableOne might have 5 people, tableTwo might have 7, etc. I know that I could do this in Node by using my capacity field and performing a query before I relate a guest to a table, but I was wondering if there was a way to do this on the database side as I'd imagine this would be better practice?
All thoughts welcome,
Thanks
I would like to sort my Group-Model by the name of the departure_country with mongoose-paginate and NodeJS.
My Group-Schema:
var GroupSchema = new Schema({
name: String,
flight_date: Date,
....
departure_country: {type: Schema.ObjectId, ref: 'Country'},
....
});
The Country-Schema:
var CountrySchema = new Schema({
name: String,
code: String,
});
Using the sortBy-option of mongoose-paginate sorts by the country's _ids and I don't know how to tell it to sort by the name.
how about making the country code the _id of Country-Schema?
then departure_country: {type: String, ref: 'Country'}
That might actually be a better design. Country-Schemas _id would be small, and country codes shouldn't have collisions. Also in GroupSchema you'd use less space to store the "foreign key" departure_country.
then you'd be able to sort by departure_country and there's a chance the sort would also be valid for country name.
I always have a certain fixed structure in my model (GroupName) and a dynamic part of 1-x (Members).
Group1
GroupName
Member 1
Member 2
Group2
GroupName
Member 1
Group3
GroupName
Member 1
Member 2
Member 3
Is it better to use two tables and connect them later via ids like this:
Groups:
Group1
GroupName
GroupId
Group2
GroupName
GroupId
Members:
Member 1
GroupId
Member 2
GroupId
or to use Schema.Types.Mixed(or anything else)? And how to do it in the second way?
I will always use them in combination later. From a gut feeling I would choose the first method:
http://blog.mongolab.com/2013/04/thinking-about-arrays-in-mongodb/
EDIT:
But even on the second method I have the issue, that one member can belong to multiple groups and I don't want to store him twice. The groups are unique and do only exist once.
But I'm new to MongoDb so I want to learn what's the best option and why.
EDIT II:
I have choosen two divide it into two docs. Is this implementation of the Schemas than correct like this:
var mongoose = require('mongoose');
// define the schema for group model
var groupSchema = mongoose.Schema({
href: {
type: String,
required: true,
unique: true
},
title: String,
members: [id: Schema.Types.ObjectId, name: String]
});
// create the model for users and expose it to our app
module.exports = mongoose.model('group', groupSchema);
&&
var mongoose = require('mongoose');
// define the schema for member model
var memberSchema = mongoose.Schema({
id: {
type:Schema.Types.ObjectId,
required: true,
unique: true
},
amount: String,
name: String
});
// create the model for users and expose it to our app
module.exports = mongoose.model('member', memberSchema);
There is an excellent post on the MongoDB blog which tells us about the various ways a schema can be designed based on the model relationships.
I believe the best schema for you would be to make use of embedded arrays with the member IDs.
//Group
{
_id: '1234',
name: 'some group',
members : [
'abcd',
'efgh'
]
}
EDIT
There is a correction needed in the schema:
// define the schema for group model
var groupSchema = mongoose.Schema({
href: {
type: String,
required: true,
unique: true
},
title: String,
members: [{id: Schema.Types.ObjectId, name: String}] //Needs to be enclosed with braces
});
// create the model for users and expose it to our app
module.exports = mongoose.model('group', groupSchema);
I don't know what your documents contains and if members are a growing array - for example Group1 can have 1-n members in any given moment . if this is the case you should go with option 2: try something like:
{gId: 1, mId: 5}
That is a design best suited for Social graph. Your Group documents will have a fixed size which is good for memory and you can easily get all the members of a group (just don't forget to index gId and mId)
If for each group there is a fixed number of members (or not growing and shrinking to much) then go with option 1
There is a great post by mongoDb team (and also src code) that talks about design.
Socialite