I have three collections in my Database: Apps, Reviews, and Users.
Apps will have multiple reviews, reviews will have one user, and users will have multiple reviews.
I do need them all tied together and am struggling. I have been able to attach reviews to users (and vice versa), but I am unable to attach the reviews to the apps. I was thinking this action needs to happen on the POST to the database for a review.
I do have the appId required (and it's stored within the review body), but I am unable to correctly identify and update the correct collection (applications) in the database. I seem to be spinning myself in circles over this.
Basically, I need to reference both the Application the review is for, and the User who reviewed when a review is posted. I do have all models "correctly" referencing the other collections (as you demo'd with your messages and users components).
Would anyone be able to point me in the right direction? Here is my code for the POST so far.
// SAVE A REVIEW TO DATABASE
router.post('/', function(req, res, next) {
var decoded = jwt.decode(req.query.token);
User.findById(decoded.user._id, function(err, doc) {
if (err) {
return res.status(404).json({
title: 'An error occurred',
error: err
});
}
var review = new Review({
content: req.body.content,
rating: req.body.rating,
app: req.body.appId,
user: doc
})
Application.findById(req.body.appId, function(err, doc) {
if (err) {
return res.status(404).json({
title: 'An error occurred',
error: err
});
}
review.save(function(err, result) {
if (err) {
return res.status(404).json({
title: 'An error occurred',
error: err
});
}
review.update(function(err, result) {
if (err) {
return res.status(404).json({
title: 'An error occurred',
error: err
});
}
doc.reviews.push(result);
doc.save();
res.status(201).json({
message: 'Saved review',
obj: result
});
});
});
});
MODELS:
Review Model:
var schema = new Schema({
content: {type: String, required: true},
rating: {type: Number},
dateCreated: {type: String},
app: {type: Schema.Types.ObjectId, ref: 'Application'},
user: {type: Schema.Types.ObjectId, ref: 'User'}
});
Application Model:
var schema = new Schema({
name: {type: String, required: true},
description: {type: String},
reviews: [{type: Schema.Types.ObjectId, ref: 'Review'}],
})
User Model:
var schema = new Schema({
firstName: {type: String, required: true},
lastName: {type: String, required: true},
password: {type: String, required: true},
passwordConfirm: {type: String, required: true},
email: {type: String, required: true, unique: true},
reviews: [{type: Schema.Types.ObjectId, ref: 'Review'}]
});
Perhaps I am going the way wrong way about this, but I seem to be pretty lost.
Thank you everyone in advance.
Related
how to create an array of objects with default values?
const schema = new mongoose.Schema({
login: {type: String, unique: true, required: true},
password: {type: String, required: true},
chatlist: [
{
channelname: {type: String, default: "frontend"},
channelid: {type: String, default: "619a6bfe5b0931f1e5dbaf2c"}
},
{
channelname: {type: String, default: "backend"},
channelid: {type: String, default: "619a71002954ba23a951bb8f"}
},
{
channelname: {type: String, default: "devops"},
channelid: {type: String, default: "619d69c190a85a40893b6522"}
},
]
})
this code above does not work
when a new user register i want to add a default chats to his profile
next thing is the user must be able to add/remove chats from the database
how should i do this? do the chats need to be an objects or documents?
const schema = new mongoose.Schema({
chatlist: {
type: Array,
defualt: []
}
})
so to have a structure in the array I personally would make the request correct using error handling.
for more information visit this site
didnt find any good solution so i just did this
const schema = new mongoose.Schema({
login: {type: String, unique: true, required: true},
password: {type: String, required: true},
chatlist: [{
channelname: String,
channelid: String
}]
})
const user = await MONGO_user.create({
login: login,
password: hashPassword,
})
user.chatlist.push(
{
channelname: "frontend",
channelid: "619a6bfe5b0931f1e5dbaf2c"
}, {
channelname: "backend",
channelid: "619a71002954ba23a951bb8f"
}, {
channelname: "devops",
channelid: "619d69c190a85a40893b6522"
})
await user.save()
I want to populate the details of products in my order. Currently it is only adding product id inside the products array. I tried a couple of methods but none seems to work.
import mongoose from 'mongoose'
const CartItemSchema = new mongoose.Schema({
product: {type: mongoose.Schema.ObjectId, ref: 'Product'},
quantity: Number,
shop: {type: mongoose.Schema.ObjectId, ref: 'Shop'},
status: {type: String,
default: 'Not processed',
enum: ['Not processed' , 'Processing', 'Shipped', 'Delivered', 'Cancelled']}
})
const CartItem = mongoose.model('CartItem', CartItemSchema)
const OrderSchema = new mongoose.Schema({
products: [CartItemSchema],
customer_name: {
type: String,
trim: true,
required: 'Name is required'
},
customer_email: {
type: String,
trim: true,
match: [/.+\#.+\..+/, 'Please fill a valid email address'],
required: 'Email is required'
},
delivery_address: {
street: {type: String, required: 'Street is required'},
city: {type: String, required: 'City is required'},
state: {type: String},
zipcode: {type: String, required: 'Zip Code is required'},
country: {type: String, required: 'Country is required'}
},
payment_id: {},
updated: Date,
created: {
type: Date,
default: Date.now
},
user: {type: mongoose.Schema.ObjectId, ref: 'User'}
})
const Order = mongoose.model('Order', OrderSchema)
export {Order, CartItem}
I tried doing this:
const create = async (req, res) => {
try {
req.body.order.user = req.profile;
console.log(req);
let order = new Order(req.body.order);
let neworder = await Order.findById(order._id)
.populate("products.product", "name price")
.populate("products.shop", "name")
.exec();
// console.log(order);
let result = await order.save();
sendMail(order);
res.status(200).json(result);
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err),
});
}
};
Also tried using this for poulating the product details but doesnt seem to work!
Order.findById(order._id).populate({ path: "products.product", select: "_id name price" })
I am using Mongoose and MongoDB v. 6.4.1. I have defined a document collection with embedded subdocuments using the following Mongoose schemas:
import mongoose, { Collection } from 'mongoose';
const connectStr = 'mongodb://localhost/appdb';
mongoose.set('useFindAndModify', false);
//Open connection to database
mongoose.connect(connectStr, {useNewUrlParser: true, useUnifiedTopology: true})
.then(
() => {console.log(`Connected to ${connectStr}.`)},
err => {console.error(`Error connecting to ${connectStr}: ${err}`)}
);
//Define schema that maps to a document in the Users collection in the appdb
//database.
const Schema = mongoose.Schema;
const roundSchema = new Schema({
date: {type: Date, required: true},
course: {type: String, required: true},
type: {type: String, required: true, enum: ['practice','tournament']},
holes: {type: Number, required: true, min: 1, max: 18},
strokes: {type: Number, required: true, min: 1, max: 300},
minutes: {type: Number, required: true, min: 1, max: 240},
seconds: {type: Number, required: true, min: 0, max: 60},
SGS: {type: Number,
default: function(){return (this.strokes * 60) + (this.minutes * 60) + this.seconds}
},
notes: {type: String, required: true}
});
const userSchema = new Schema({
id: {type: String, required: true}, //unique identifier for user
password: String, //unencrypted password (for now!)
displayName: {type: String, required: true}, //Name to be displayed within app
authStrategy: {type: String, required: true}, //strategy used to authenticate, e.g., github, local
profileImageUrl: {type: String, required: true}, //link to profile image
rounds: [roundSchema],
securityQuestion: {type: String},
securityAnswer: {type: String, required: function() {return this.securityQuestion ? true: false}}
});
//Convert schema to model
const User = mongoose.model("User",userSchema);
In an Express.js GET route, I am using the following code to query for a specific document:
try {
let thisUser = await User.findOne({id: req.params.userId});
console.log("thisUser: " + JSON.stringify(thisUser));
if (!thisUser) {
return res.status(400).send("No user account with specified userId was found in database.");
} else {
return res.status(200).json(thisUser.rounds);
}
} catch (err) {
console.log(err);
return res.status(400).message("Unexpected error occurred when looking up user in database: " + err);
}
My console.log statement confirms that the above route in fact obtains the desired document, e.g.:
thisUser: {"_id":"5e6704234f3864318caedd12","id":"chundhau#gmail.com","password":"GoCougs20","displayName":"chundhau#gmail.com","authStrategy":"local","profileImageUrl":"https://www.gravatar.com/avatar/4b565c54d37b3f5ad4caa1c129e865b8","securityQuestion":"First pet?","securityAnswer":"Daisy","__v":0,"rounds":[]}
When I look at this same document in MongoDB Compass Community, I can confirm that its rounds subdocument array has several elements:
However, as shown in the console.log output above, rounds is coming back as an empty array. I have confirmed that (a) rounds is in fact an array (using Array.isArray()) and that (b) rounds has no elements (thisUser.rounds.length === 0).
Shouldn't I be able to access all of the subdocuments through thisUser.rounds? What have I done wrong?
I have discovered a solution. I changed:
let thisUser = await User.findOne({id: req.params.userId});
to
let thisUser = await User.findOne({id: req.params.userId}).lean();
Miraculously, thisuser.rounds was no longer empty. It instead contained all of the array elements I could see when I inspected the document in MongoDB Compass Community!
While this solution worked, I do not know why it worked. If anyone could help me understand what's going on here, I'd appreciate it!
**I have answered below. In short you need to require the Model in the module in which you wish to populate, even though you do not refer to it directly.
I am hitting a strange problem with mongoose when populating just one particular array of IDs.
I have three models, User, Company and Widgets.
When I return the company populated with the users all is fine using:
Company.findOne({ name: 'xyz' })
.populate('users')
.exec(function(err, company) {
if (err) return res.send(err)
res.send(company)
})
However when I try to replace populate 'users' with 'widgets' I get the following error:
{
"message": "Schema hasn't been registered for model \"widget\".\nUse mongoose.model(name, schema)",
"name": "MissingSchemaError"
}
Here are the models:
USER:
const UserSchema = new Schema({
name: String,
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
company: {
type: Schema.Types.ObjectId,
ref: 'company'
}
});
const User = mongoose.model("user", UserSchema);
COMPANY:
const CompanySchema = new Schema({
name: String,
URL: {
type: String,
unique: true
},
users: [{
type: Schema.Types.ObjectId,
ref: 'user'
}],
widgets: [{
type: Schema.Types.ObjectId,
ref: 'widget'
}]
});
const Company = mongoose.model('company', CompanySchema);
WIDGET:
const WidgetSchema = new Schema({
title: {
type: String,
required: true
},
maker: String
});
const Widget = mongoose.model('widget', WidgetSchema);
I have manually inspected the _ids in the widget array of the company model and they are all correct.
OK, so this was a lack of understanding on my behalf.
In the module where I was using:
Company.findOne({ name: 'xyz' })
.populate('users')
.exec(function(err, company) {
if (err) return res.send(err)
res.send(company)
})
I had imported the User model for other uses in the module. However, as I was not directly referring to Widget I had not imported it. Having done some more research I found that you need to import a model when populating even though not referring to it directly.
Let me know if best to delete whole thread or leave for reference.
Hello guys i am having a problem getting from database only fields that are permitted by the user.
So my schema is:
var profileSchema = mongoose.Schema({
authId: {type: Schema.Types.ObjectId, ref: 'Auth'},
fname: String,
lname: String,
am: Number,
email: String,
location: String,
languages: [String],
birth_date: {
type: Date,
default: Date.now
},
reg_date: {
type: Date,
default: Date.now
},
last_log: {
type: Date,
default: Date.now
},
permissions: {
location: {type: Boolean,Default:true},
birth_date: {type: Boolean,Default:true},
email: {type: Boolean,Default:true},
am: {type: Boolean,Default:true},
subjects: {type: Boolean,Default:true},
files: {type: Boolean,Default:true},
posts: {type: Boolean,Default:true}
},
ip: String,
subjects: [{type: Schema.Types.ObjectId, ref: 'Subject'}],
files: [{type: Schema.Types.ObjectId, ref: 'FileIndex'}],
posts: [{type: Schema.Types.ObjectId, ref: 'Post'}],
notifications: [{type: Schema.Types.ObjectId, ref: 'Notifications'}]
});
And I am trying to get only fields that in permission field have true which means it's permitted. So I am running the following query :
database.Profile.findOne({_id: req.params.id}).exec(function (err, profile) {
console.log(profile);
res.send(profile);
});
How do I select only the fields that are permitted?
Try this, It ,might get you what you want:
database.Profile.findOne({_id: req.params.id},{location:$.permissions.location , birth_date:$.permissions.birth_date, email:$.permissions.email, am:$.permissions.am, subjects:$.permissions.subjects, files:$.permissions.files, posts:$.permissions.posts}).exec(function (err, profile) {
console.log(profile);
res.send(profile);
});
You could do a query with the lean() method chained since documents returned from queries with the lean option enabled are plain javascript objects, not MongooseDocuments and manipulate the object returned by removing the keys as determined by the permissions object fields:
Object.filter = function( obj, predicate) {
var result = {}, key;
for (key in obj) {
if (obj.hasOwnProperty(key) && !predicate(obj[key])) {
result[key] = obj[key];
}
}
return result;
};
database.Profile.findOne({_id: req.params.id}).lean().exec(function (err, doc) {
if (err) return handleError(err);
console.log(doc);
console.log(doc.permissions);
var filtered = Object.filter(doc.permissions,
function (key){ return doc.permissions[key]; }
);
console.log(filtered);
res.send(filtered);
});
Another alternative that uses the Mongoose's projection select() method would be to make two queries; the first one will return the whole document and the next will query the same document but project the fields based on the permissions object fields:
The following shows this:
database.Profile.findOne({_id: req.params.id}).lean().exec(function (err, doc) {
console.log(doc);
console.log(doc.permissions);
var projection = Object.keys(doc.permissions)
.filter(function (key){ return doc.permissions[key]; })
.join(' ');
console.log(projection);
database.Profile.findOne({_id: req.params.id})
.select(projection)
.exec(function (err, profile) {
if (err) return handleError(err);
res.send(profile);
});
});