So currently I am using this
await User.findOneAndUpdate({email: req.user.email }, {lastLoad: 'dfConsignee'});
But I want to be able to update all users with the same truckNumber to be updated when the form is submitted.
SO I tried using this
await User.update({truckNumber: truckNumber }, {lastLoad: 'dfConsignee'});
But this only updated the active user for some reason. How would I go about updating all the accounts with the same truck number on form completion?
Thank you guys in advance <3
Edit ----------------------------------------------------------------------------------------------------------------------------
By active user, I meant logged in user.
Mongoose Schema:
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
isVerified: {
type: Boolean,
default: false
},
profilePic: {
type: String,
default: 'abc'
},
truckDriver: {
type: Boolean,
default: false
},
truckNumber: {
type: Number
},
admin: {
type: Boolean,
default: false
},
customer: {
type: Boolean,
default: false
},
loadPlanning: {
type: Boolean,
default: false
},
lastLoad: {
type: String,
default: "dfConsignee"
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
Sample Document:
{"_id":{"$oid":"aaaaaaaaaaaaaaaa"},
"isVerified":true,
"profilePic":"abc",
"truckDriver":true,
"admin":false,
"customer":false,
"loadPlanning":false,
"lastLoad":"aAtShipper",
"name":"Nicholas Cage",
"email":"NicholasCage#thegreatestactorever.com",
"password":"password",
"date":{"$date":{"$numberLong":"1580858993547"}},"__v":{"$numberInt":"0"},
"truckNumber":"123",}
To add a bit more clarification, when the form is submitted, it will pull the logged in users truck number. When the form is submitted, I am wanting everyone who shares that same truck number to have their value in lastLoad be updated as well.
Try using updateMany(). The resulting code would look something like this:
User.updateMany({truckNumber: truckNumber}, {lastLoad: 'dfConsignee'});
If you really want to familiarize yourself with Mongoose and MongoDB as a whole, I'd suggest taking a look at the API docs for Mongoose and the MongoDB Manual, it goes into much more detail about the tools you have available to you when using MongoDB. That's how I found the answer to this question!
OMG Finally figured this out :D
I thought that when I restarted Nginx it would restart the app running on the server ( I would see front end changes doing this, but the back end changes wouldn't show up), once I restarted PM2 all the back end changes came along with it.
Thank you everyone for the help :D
Now "await Users.updateMany({truckNumber: req.user.truckNumber}, {lastLoad: 'aAtShipper'}" will update all accounts with a shared truck Number :D
Related
I am trying to let users send friend requests to other users similar to that of Facebook and other social media platforms. I have started creating this functionality, but quickly got stuck since I am new to mongoDB and the whole Schema thing.
Here are the models I currently have:
// User Schema
var UserSchema = new Schema({
_id: {
type: Number
},
name: {
type: String
},
username: {
type: String,
index: true
},
password: {
type: String,
required: true
},
email: {
type: String
},
friends: [{
friendName: {
type: Schema.ObjectId,
required: true,
ref: 'User'
},
duration: {
type: Number
}
}]
});
// Friendship Schema
var FriendshipSchema = new Schema({
participants: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
requestTo: {
type: Schema.Types.ObjectId,
ref: 'User'
},
accepted: {
type: Boolean,
default: false
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
var Friendship = module.exports = mongoose.model('Friendship', FriendshipSchema);
var User = module.exports = mongoose.model('User', UserSchema);
This is as far as I have gotten. From here, I do not know how to use these schemas to establish friendships between 2 users. My ultimate goal is to have a button on a webpage that sends a friend request to the intended recipient, where they can then accept or deny the request.
Any help with this would be awesome, since I do not know what to do from here with these 2 schemas. Thanks!
We would need to take one schema only which is userSchema(as is Israel said above, you only need an array/object to list your friendship on the userSchema). But we will need to add another schema(said it friendRequestSchema).
FriendRequest schema would be:
- ID user request (int)
- ID user recipient (int)
- status (int) //let say 1= requested, 2=accepted, 3=rejected
And the controller it should be from the user A click "Friend Request" button on your user B page.
Friend Request Button will call a function let saying it "sendFriendRequest()"
If function running it would be recorded on friendRequest DB, which is will record ID of user A(as requester), ID of user B and request status.
If request status = 1 then user B will be notified and give him two option which is accepted and rejected.
User B accept or reject it
If user press button accept, then the status updated in friendRequest DB to be=> 2 (Accepted). Then, you have to call another function to add user ID A to friendship list of User B. Conversely. Then if you want to make a notification you can call it as well.
Else user B will press reject (status will be => 3) then notif it.
UserSchema
var UserSchema = new Schema({
name: String,
username: {
type: String,
index: true
},
password: {
type: String,
required: true
},
email: String,
friendship: [String] });
Then FriendRequestschema
var FriendRequestSchema = new Schema({
requester: {
type: int,
required: true
},
recipient: {
type: int,
required: true
},
status:
type: int,
required: true });
This just to let you know, how its work. More complex method about (sendrequest,block .etc) you can check this link, It's flow process for PHP, but you can easily move it to your js. Hope it help you man.
Your model can be improved, and your code can be cleaned:
First, you don't need the brackets if you only give type for the field:
var UserSchema = new Schema({
name: String,
username: {
type: String,
index: true
},
password: {
type: String,
required: true
},
email: String,
friends: [String]
});
This should be a simplified version of your schema. The _id field doesn't need to be specified because mongoose creates it automatically. If you wanna put a customized value there, just do it when you insert.
Second:
If you wanna reference other users, why not to use only a simple array that contains ids from other users. For example, if you have user A, the "friendship" of this user are user ids contained in his "friends" field.
{id:12345, username:"A", password:***, email:"a#fakemail.com", friends:[B_id,C_id,D_id]}
In that case, whenever you wanna make a list of friends of A, you can just perform a $lookup operation in mongodb and it will fill the other users information for you.
I don't think I covered all of your questions, but I hope my answer was helpful.
I have an application with 2 roles, a User and a Photographer. the difference between these 2 models is a isAdmin field in User and a Photo[ ] in Photographer and a order[ ] in User. But I need only one login for both of them! Clearly, need different views and routs for them! how can I do this? If any guide, I would be appreciated! Thanks a lot!
The schemas are as below:
var userSchema = new Schema({
//some other fields, the same with photographer
isAdmin: { type: Boolean, default: false },
orders: [{
price: { type: Number, default: 0 }
},
{timestamps: true}
]
});
var photographerSchema = new Schema({
//some other fields, the same with user
photos: [{
title: { type: String, default: '' },
path: { type: String },
price: { type: Number, default: 0 },
isAppoved: { type: Boolean, default: false },
},
{timestamps: true}
]
});
You should rethink your architecture. Better solution would be to have 1 User model and 2 profile submodels. User model would contain the type of the user and fetch profile info(writer_profile/user_profile) from the submodels.
If you won't refactor, this will turn in to a mess pretty fast. Think about it, what happens when a User and a Writer registers with the same credentials? You will need to check that too.
Im using MongoDb, and I have a workspace schema with mongoose (v4.0.1):
var Workspace = new mongoose.Schema({
name: {
type: String,
required: true
},
userId: {
type: String,
required: true
},
createdOn: {
type: Date,
"default": Date.now
}
});
And a user schema:
var User = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true
},
organisation: {
type: String,
required: true
},
location: {
type: String,
required: true
},
verifyString: {
type: String
},
verified: {
type: Boolean,
default: false
},
password: {
type: String,
required: true
},
createdOn: {
type: Date,
"default": Date.now
},
isAdmin: {
type: Boolean,
default: false
}
});
So the Workspace userId is the ObjectID from the User document.
When Im logged in as an adminstrator, I want to get all workspaces, as well as the email of the user that owns the workspace.
What Im doing is getting very messy:
Workspace.find({}).exec.then(function(workspaceObects){
var userPromise = workspaceObects.map(function(workspaceObect){
// get the user model with workspaceObect.userId here
});
// somehow combine workspaceObjects and users
});
The above doesnt work and gets extremely messy. Basically I have to loop through the workspaceObjects and go retrieve the user object from the workspace userId. But because its all promises and it becomes very complex and easy to make a mistake.
Is there a much simpler way to do this? In SQL it would require one simple join. Is my schema wrong? Can I get all workspaces and their user owners email in one Mongoose query?
var Workspace = new mongoose.Schema({
userId: {
type: String,
required: true,
ref: 'User' //add this to your schema
}
});
Workspace.find().populate('userId').exec( (err, res) => {
//you will have res with all user fields
});
http://mongoosejs.com/docs/populate.html
Mongo don't have joins but mongoose provides a very powerfull tool to help you with you have to change the model a little bit and use populate:
Mongoose population
You have to make a few changes to your models and get the info of the user model inside your workspace model.
Hope it helps
This question already has answers here:
mongoose. updating embedded document in array
(3 answers)
Closed 7 years ago.
I have a nested item schema in an invoice schema as given below :
var InvoiceSchema = new db.Schema({
title: {
type: String,
required: true
},
description: String,
clientId: db.Schema.ObjectId,
companyId: db.Schema.ObjectId,
poNumber: Number,
invoiceNumber: {
type: Number,
default: 1000
},
taxApplied: {
type: Boolean,
default: false
},
feeApplied: {
type: Boolean,
default: false
},
lastUpdated: {
type: Date,
default: Date.now
},
createdOn: {
type: Date,
default: Date.now
},
status: {
type: String,
enum: ['presented', 'entered', 'hold', 'paid',
'partially paid', 'reversed', 'void'],
required: true
},
invoiceItems: [InvoiceItemSchema]
});
var InvoiceItemSchema = new db.Schema({
invoiceId: {
type: db.Schema.ObjectId,
required: true
},
description: String,
qty: Number,
rate: Number,
isFlatFee: {
type: Boolean,
default: false
}
});
I am able to create a new invoiceItem and push it directly into the invoice invoiceItems array, and read it when it's in there, but I am having a lot of trouble trying to update and delete. I was looking at this site here
http://tech-blog.maddyzone.com/node/add-update-delete-object-array-schema-mongoosemongodb
But I couldn't seem to get any of that to work.
I know in that blog he is using $set to update things but it looks like it only updates one field and I want to update any field based on user input and not a hard coded field. I don't have any code to show right now because I am quite lost but some help would be appreciated!
If you have a reference to the Invoice from your InvoiceItem you shouldn't really need to save the items in a list in Invoice. If you want to get that list you could simple do a query like:
InvoiceItem.find({invoiceId: invoiceId}, function(err, items) {
...
})
Now this will make it much easier to update as you only need to update each InvoiceItem object and just keep the reference to the Invoice.
Hope this helped you out.
I want to display friends of Authenticated user in angularjs page.
// Find a list of Friends
$scope.find = function() {
$scope.friends = Authentication.user.friends;
$scope.firstFriendName = Authentication.user.friends;
};
I'm using mongoose with nodejs.(MeanJS)
How can I populate friends of current user in meanjs?
Thanks.
Either extend the user object or add a custom model for example Person , that consists of both user reference, and list of friends:
/**
* Person Schema
*/
var PersonSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Person name',
trim: true
},
desc:{
type: String,
default: '',
trim: true
},
friends:[{
rate: Number,
date: Date
}],
,
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Person', PersonSchema);
Then write the corresponding controller and views. Or just use meanJS generators:
yo meanjs:crud-module Persons
Then add the appropriate changes to the module. By the way, there is a package for something similar that seems to patch the User schema: moongose-friends
var PersonSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Person name',
trim: true
},
desc:{
type: String,
default: '',
trim: true
},
friends:[{
type: Schema.ObjectId,
ref: 'User'
}],
,
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
Then you would use the populate() method built into Mongoose which MEANJS uses. You can actually see how this works if you use the generators to build a CRUD module. Then you can look at the express controller for a specific view and you will see how it uses populate() (this is pretty much what you will see for a list function in the express controller when you generate a crud module.
Friends.find().sort('-created').populate('user', 'displayName').exec(function(err, friends) {
// Handle error & return friends code here
});
Hope that helps. You would then want to look at the angular controller and view so that you could modify the object in the controller and reflect it in the view.