I have an object called company that inside I have name(String) and locations(Array)
Inside locations I want to have a key called name that the user will generate, and second key generated by using ObjectID.
Unfortunately I can't get this to work as expected.
Example from Postman. Note that the locations didn't get _id.
What is going wrong?
My model
const mongoose = require('mongoose')
const companySchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
locations: [
{
_id: mongoose.Schema.Types.ObjectId,
name: String
}
]
})
module.exports = mongoose.model('Company', companySchema)
My controller
createCompany: (req, res) => {
const { name, locations } = req.body
const company = new Company({
_id: new mongoose.Types.ObjectId(),
name: name,
locations: locations
})
company
.save()
.then(() => {
res.status(200).json({
company
})
})
.catch(error => {
res.status(500).json({
error
})
})
},
Mongoose will create _id for the top level and nested array of documents itself, which is a default behaviour.
So defining schema with only the fields that you want should be it.
const companySchema = new mongoose.Schema({
name: String,
locations: [
{
name: String
}
]
});
module.exports = mongoose.model("Company", companySchema);
OR
const locationSchema = new mongoose.Schema({
name: String
});
const companySchema = new mongoose.Schema({
name: String,
locations: [locationSchema]
});
module.exports = mongoose.model("Company", companySchema);
And subsequently you don't need to create the _id from the Model object as well.
const company = new Company({
name: name,
locations: locations
});
company
.save()
.then(() => {})
.catch(e => {});
Related
I am trying to add an item to a MongoDB array with RESTAPI through Axios. I thought it would look similar to the push method but I have no idea how to do that.
my Model is of a person:
const Schema = mongoose.Schema;
const PersonSchema = new Schema({
name: String,
password: String,
friends: [],
missions: []
})
const personModel = mongoose.model('Person', PersonSchema);
I want to add a mission to the mission array of a person.
and for example, in order to add a new Person, I use NodeJS and API:
(api.js)
router.post('/api/people', (req, res) => {
const personToAdd = req.body;
const newPersonPost = new personModel(personToAdd);
newPersonPost.save((e) => {
if (e) {
console.log("error");
}
});
res.json({
msg: 'Received'
})
});
and in the client side I use Axios:
axios({
url: 'http://localhost:8080/api/people',
method: 'POST',
data: dataToUpdate
})
.then(() => {
console.log('axios sent info to server');
}).catch((e) => {
console.log('error' + e);
})
Thank you so much!
express
router.post('updating mission endpoint url', async (req, res) =>
try {
const query = { /* content */}; /* write a query to retrieve the concerned user by using a unique identifier */
let person = await personModel.findOne(query);
person.missions.push(req.body.mission);
personModel.save();
} catch (err) {
console.log(err);
}
});
client
In the client side you just have to put the mission you want to add in data like you did above with the right endpoint url and you should add a unique identifier for the user you want to add mission to.
[] will not assign array type to your variable.
Change your schema file with the following:
const Schema = mongoose.Schema;
const PersonSchema = new Schema({
name: { type: String },
password: { type: String },
friends: { type: Array },
missions: { type: Array }
})
Update the db model entity file with following
First method:
const Schema = mongoose.Schema;
const PersonSchema = new Schema({
name: String,
password: String,
friends: {type : Array},
missions: {type : Array}
})
const personModel = mongoose.model('Person', PersonSchema);
Second Method :
const Schema = mongoose.Schema;
const PersonSchema = new Schema({
name: String,
password: String,
friends: [{ type: String }],
missions: [{ type: String }]
})
const personModel = mongoose.model('Person', PersonSchema);
You can update the array object as per your requirements.
You just want to be using the $push update operator, very simple, like so:
db.collection.updateOne(
{
_id: user._id
},
{
"$push": {
"missions": {
mission: newMission
}
}
})
Mongo Playground
I'm creating the backend of my project. In this project, there are some groups, and each groups has its partecipant. I would like to make a function in nodejs that retrive all the groups that, in the partecipant field(which is an array), has a given user.
Here is the schema of group:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const GroupSchema = new Schema({
name: {
type: String,
required: true,
unique: true,
},founder:{
type:String,
required: true,
}, partecipants:{
type: Array,
required:false,
}, files: {
type:Array,
required: false,
}
})
const Group = mongoose.model('Group', GroupSchema);
module.exports = Group;
For now, I wrote only this:
const getGroupByUser = (req, res) => {
const user = req.body.user;
Group.find()
.then(files => res.json(files))
.catch(err => res.status(400).json('ERROR:'+err))
}
But it obviusly returns all the groups. I don't know if there is a way to make it in the promise, or if I have to cycle throught the array, like a linear search, but for multiple cases. Any idea? Thank you
const getGroupByUser = (req, res) => {
const user = req.body.user;
const id = req.query.id;
// if id is saved as objectId covert it using Types.ObjectId(id)
Group.find({partecipants: id})
.then(files => res.json(files))
.catch(err => res.status(400).json('ERROR:'+err))
}
using {partecipants: id} you tell to mongoDb to find all group that have into the array the userId you pass in query params
Its been a tough time for me trying to figure this out.
The problem I'm trying to solve is this:
I have USERS who has list of COMPANIES,
this COMPNAY has list of PROJECTS etc.
I'm trying to create a PROJECT under a COMPANY and a COMPANY under a USER using referencing.
This USER is going to be populated from form fields likewise COMPANY PROJECT
USERS SCHEMA
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//============= User Schema============//
const userSchema = new Schema({
name: String,
email: String,
//referencing user's company/companies
company: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'company',
},
],
},{
timestamps: true
});
const Users = mongoose.model('user', userSchema);
module.exports = Users;
COMPANY SCHEMA
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//======== Company schema ========//
const compSchema = new Schema({
company_name: String,
//referencing user schema
user: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
],
//referencing projects
projects: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'project',
},
],
},{
timestamps: true
});
const Company = mongoose.model('company', compSchema);
module.exports = Company;
PROJECT SCHEMA
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//PROJECT SCHEMA
const projectSchema = new Schema({
project_title: String,
//referencing company schema
company: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'company',
}
],
},{
timestamps: true
});
const Projects = mongoose.model('project', projectSchema);
module.exports = Projects;
CONTROLLERS
const Users = require('./userSchema');
const Company = require('./companySchema');
const Projects = require('./projectSchema');
//create user
exports.createUser = async (req, res) => {
const data = {
name: req.body.name,
email: req.body.email,
}
Users.create(data, (err, done) => {
if(err) return err;
res.json({UserData: done});
return;
});
}
exports.getAllUsers = async(req, res) => {
}
//create company
exports.createCompany = async (req, res) => {
const CompanyData = {
company_name: req.body.company_name,
}
Company.create(companyData, (err, done) => {
if(err) return err;
res.json({CompanyDetails: done});
return;
});
}
exports.getllCompany = async (req, res) => {
}
//create project
exports.createProject = async (req, res) => {
const projectData = {
project_title: req.body.project_title,
}
Projects.create(projectData, (err, done) => {
if(err) return err;
res.json({ProjectDetails: done});
return;
});
}
exports.getAllProjects = async (req, res) => {
}
I understand that populate can be used to push documents to children. Any assistance offered will be highly appreciated
Store reference
Firstly, while creating a Company inside createCompany controller, you should add user _id in users array like this:
const CompanyData = {
company_name: req.body.company_name,
users: [req.body.userId] // Lets assume, user _id is coming in request body
}
Company.create(companyData, (err, done) => {
// Your code
});
Then, when you create a Project, add _id of Company you just created, in createProject controller like this:
const projectData = {
project_title: req.body.project_title,
company: [req.body.comapnyId] // Again assuming, company _id is coming in request body
}
Projects.create(projectData, (err, done) => {
// Your code
});
Update reference
Also, as I see in your schemas, you are trying to keep it bidirectional, like user's _ids in company doc & company's _ids in user doc.
To achieve this, if you create a user, you should update the corresponding company with pushing(addToSet actually) new user _id in user array.
Example:
To create a user in controller, run two scripts synchronously one-by-one:
Create a user with company _id and other details.
const data = {
name: req.body.name,
email: req.body.email,
company: [req.body.companyId]
}
Users.create(data, (err, done) => {
// Your code
});
Update company doc(whose _id is saved in above user) with _id of newly created user.
const updateCompany = { "$addToSet": { user: user._id } }; // Get `user._id` from above synchronous script
Company.update({_id: req.body.companyId}, data, (err, done) => { // Updated filter
// Your code
});
This is my table schema
var mongoose=require("mongoose");
var tableSchema=new mongoose.Schema({
tName:String,
keys:[
{
type:mongoose.Schema.Types.ObjectId,
ref:"key"
}
],
fields:[
{
type:mongoose.Schema.Types.ObjectId,
ref:"field"
}
]
})
module.exports=mongoose.model("table",tableSchema);
----Key Schema
var mongoose=require("mongoose")
var keySchema=new mongoose.Schema({
name:[String],
value:[String]
})
module.exports=mongoose.model("key",keySchema);
---field Schema
var mongoose=require("mongoose")
var fieldSchema=new mongoose.Schema({
name:[String],
value:[String]
})
module.exports=mongoose.model("field",fieldSchema);
----How I Pushed into
app.post("/table/:id/value",function(req,res){
var Key={
name:req.body.key,
value:req.body.keyValue
}
var Field={
name:req.body.field,
value:req.body.fieldValue
}
table.findById(req.params.id,function(err,foundTable){
if(err){
console.log(err)
}
else{
console.log(foundTable)
key.create(Key,function(err,createKey){
foundTable.keys.push(createKey)
console.log(createKey)
})
field.create(Field,function(err,createField){
foundTable.fields.push(createField)
console.log(createField)
})
foundTable.save();
console.log(foundTable);
res.redirect("/table/"+req.params.id)
}
})
})
ObjectId are not being refernced
Here is the Image that prints the table
How I populated the table
app.get("/table/:id",function(req,res){
table.findById(req.params.id).populate("keys").populate("fields").exec(function(err,foundTable){
if(err){
console.log(err)
res.redirect("/")
}
else{
console.log(foundTable);
res.render("show",{table:foundTable})
}
})
})
I Dont know where I had gone wrong,
everything seems to be fine but
the objected is not referenced when printed and
it is not being populated
How it should be printed reference: https://bezkoder.com/mongoose-one-to-one-relationship-example/
This is an example:
1st schema
const mongoose = require("mongoose");
const Customer = mongoose.model(
"Customer",
new mongoose.Schema({
name: String,
age: Number,
gender: String
})
);
module.exports = Customer;
2nd schema
const mongoose = require("mongoose");
const Identifier = mongoose.model(
"Identifier",
new mongoose.Schema({
cardCode: String,
customer: {
type: mongoose.Schema.Types.ObjectId,
ref: "Customer"
}
})
);
module.exports = Identifier;
How it should be printed
{
_id : ObjectId("5da000be062dc522eccaedeb"),
cardCode : "5DA000BC06",
customer : ObjectId("5da000bc062dc522eccaedea"),
__v : 0
}
How it should be populated
[ { _id: 5da135bf61a1dd3e9c2a6e82,
cardCode: '5DA135BD61',
customer:
{ _id: 5da135bd61a1dd3e9c2a6e81,
name: 'bezkoder',
age: 29,
gender: 'male',
__v: 0 },
__v: 0 } ]
try this .populate([ 'keys', 'fields' ])
The reason why keys and fields are not inserted is that the foundTable.save() will be executed before creating the new Key and Field documents and push there _id to the foundTable.
One way to solve the issue is by using async/await. You can modify your code as below using async/await
app.post("/table/:id/value", async function (req, res) {
var Key = {
name: req.body.key,
value: req.body.keyValue,
};
var Field = {
name: req.body.field,
value: req.body.fieldValue,
};
try {
const foundTable = table.findById(req.params.id);
const createKey = await key.create(Key);
const createField = await field.create(Field);
foundTable.keys.push(createKey._id);
foundTable.fields.push(createField._id);
await await foundTable.save();
res.redirect("/table/"+req.params.id)
} catch (err) {
console.log(err);
// handle failure here
}
});
This will make sure the Key and Field are created and _id is pushed to foundTable before saving the foundTable
Regarding the populate query. Looks like once you save the _id of Field and Key in foundTable your existing query itself should work
I have a Users model with some fields and reference to "Skills" model.
The models are look like this below
1.Users.js (users model)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UsersSchema = new Scheam({
name: { type : String },
email: { type : String },
address: { type : String },
Skills: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Skills' }],
})
module.exports = mongoose.model('Users', UsersSchema);
2.Skills Model
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const SkillsSchema = new Schema({
name : { type: String },
});
module.exports = mongoose.model('Skills', SkillsSchema);
I am creating the new user with dropdown menu of skills selection storing the id's of the skills in users model.
And getting the skills by populating the Users model like this in node js app.
export function getUser(req, res) {
console.log('getUser');
console.log('req.body',req.body);
Users.findOne({_id: req.body.user_id}).populate('skills').exec(function(err, usr_doc){
if(err) {
res.json({
status: 401,
message: 'something went wrong!',
err: err,
});
} else if (!err && usr_doc != null) {
res.json({
status: 200,
message: 'User details found!',
data: {_id: usr_doc._id, skills: usr_doc.skills,name: usr_doc.name,token: usr_doc.token},
})
}
})//Users.findOne end
}
The above code is working fine for normal rest api.
Here I try to create the Same api using GraphQL server of express-graphql in Nodejs.
The code look like this.
3.Schema.js
const graphql = require('graphql');
const Users = require('../models/Users');
const Skills = require('../models/Skills');
const {
GraphQLObjectType,
GraphQLInt,
GraphQLFloat,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLList,
GraphQLNonNull,
GraphQLBoolean,
GraphQLObject
} = graphql;
const UsersType = new GraphQLObjectType ({
name: 'Users',
fields: () => ({
id: {type: GraphQLID},
name: { type : GraphQLString },
email: { type : GraphQLString },
**skills: {
type: new GraphQLList(SkillsType),
resolve(parent, args){
//Here I need to know how to get the skills name of the users
}
}**
})
const SkillsType = new GraphQLObjectType({
name: 'Skills',
fields: () => ({
name: { type : GraphQLString },
})
})
At present I didn't use any reference id in skills model, only name. I want to get the skill names of the Users.
If i use the below, I am getting all of the skill names instead of user particular or populated skill names.
skills : {
type: new GraphQLList(SkillsType),
resolve(parent, args){
return Skills.find({})
}
Please provide any suggestions regarding this.
I can give more information if you need.
To get the skills of a user, instead of Skills.find({}) which returns all the documents present in the skills collection, add a condition there with the parent object.
The parent object, which contains result from the resolver on the parent field here. So skills, a child resolver, can be written as:
skills: {
type: new GraphQLList(SkillsType),
resolve(parent, args) {
return await Skills.find({"_id": {$in: parent.skills} }) }
}