I have a mongoDB with a mongoose schema:
const newsSchema = new Schema({
serverid: Number,
resetid: Number,
newsid: Number,
timestamp: Number,
type: Number,
win: Number,
attacker_num: Number,
attacker_name: String,
defender_num: Number,
defender_name: String,
result1: Number,
result2: Number,
a_tag: String,
d_tag: String,
killhit: Number
});
Below is what the CSV api looks like that I insert into my mongoDB:
9,672,22697434,1408587629,5,1,351,LaFing at SoF,9,ReDflag,10,0,SoL,LaF,0
9,672,22697435,1408587629,5,1,377,Commorragh,9,ReDflag,10,0,PDM,LaF,0
9,672,22697436,1408587629,5,1,589,The IX Kiss,9,ReDflag,10,0,SoL,LaF,0
Field #3 is unique from the api, it is never duplicated. In my schema it is called newsid. If my script were to update the database from the feed and it tries to insert another row that contains a newsid that is already in the database, is there a way to prevent that from happening?
A unique key constraint would do exactly what you want.
The unique key can be set in mongoose with either the schema field options:
const s = new Schema({newsid: {type: Number, unique: true}});
or by the index method:
Schema.path('newsid').index({unique: true});
If an attempt is made to create a document that already has an entry for that key then an error will be thrown:
NOTE: violating the constraint returns an E11000 error from MongoDB when saving, not a Mongoose validation error.
Related
I am trying to store the amount of time an employee has worked in my MongoDB database, but not able to make a mongoose schema whose type will object.
The desired database should have a document like this:
{
name: 'name of employee',
report: {'01-01-2023':5hr, '02-01-202':7hr, '03-01-2023':8hrs}
}
This report will contain an object whose key will be a date and the value will be minutes or hours an employee has worked on that date.
how can I make a schema to achieve the desired goal, I have tried like this but did not work.
const UserSchema = new mongoose.Schema({
name:{
type: String,
required: true
},
report: {
type: Object, // what should I write here
}
})
I couldn't understand that for what purpose mongoose schemaType is used for. If someone could explain it will be helpful.
I'm have to reference another schema from a schema i want to know if we can get the details of all schema together when we do a findOne() on mongoose.
mixed schema means whatever you want the type to be. if you input a String, Number, Date, mongoose will let you do that. However according to documentation, mongoose ref does not work with mixed.
Note: ObjectId, Number, String, and Buffer are valid for use as refs.
if you use mixed, and ref it, you won't be able to query it back.
If you start all over(delete the database and reinsert again), use ObjectId instead of Mixed.
var storySchema = Schema({
author : { type: ObjectId, ref: 'Person' },
});
If you wish to retain old database, the best way is to change mixed to string
var storySchema = Schema({
author : { type: String, ref: 'Person' },
});
I am using the mean stack. In Mongoose I defined a model with these properties:
var personSchema = new mongoose.Schema({
personName:{ type: String, unique: true, required: true, index:true },
start: { type: Date},
end: { type: Date }
});
However, when testing I realised I had made a mistake and that personName should not be unique. I removed the unique: true property and restarted MongoDB and the app.
However, I still get the duplicate key error when submitting.
Can anyone tell me what I'm doing wrong?
You might have created an index for the personName field.
Remove the index associated with field personName and try, it will work.
Reason:
when the field personName in the state "unique: true" index would be fine and now after removal of the state "unique: true". If we are trying to enter a record which is having a personName which is already there in the DB, then DB will throw Duplicate key error.
I have a schema that has a field that could reference different schema.
var HistorySchema = new Schema({
type: {type: String, required: true},
objectId: {
type: Schema.Types.ObjectId,
required: true,
},
changed: {type: Schema.Types.Mixed}
})
The documents of this schema allows me to keep track of changes happens in different types of objects with objectId.
For example, if User has changed name from 'John' to 'Steve', a History document would have:
{
type: 'User',
objectId: '55fa6bf0831ba3fa0879e7e8',
changed: {name: {oldValue: 'John', newValue: 'Steve'}}
}
Obviously, type can be many different things.
My question is, can I magically populate the objectId field without knowing type before the query?
I know I can do:
History.query({...}).populate('objectId', null, 'User').exec(...);
But that requires me to know the type is User when the query is constructed.
And obviously I can do a second query manually given the type and objectId.
For example, is it possible to save the ref type of a document (not schema) at runtime and take advantage of that? I look around and don't seem to find a way.
I am having this schema
var PostSchema = new Schema({
title: {type: String, trim: true}
, description: {type: String, trim: true}
, votes: [{ type: Schema.ObjectId, ref: 'User' }]
})
I want to sort posts based on votes i.e, I need to sort by array length.
Tried the usual way, but din't work
PostSchema.statics.trending = function (cb) {
return this.find().sort('votes', -1).limit(5).exec(cb)
}
Any help?
version of mongoose I am using is 2.7.2
You can't do that directly. To be able to sort on array length, you have to maintain it in a separate field (votes_count, or whatever) and update it when you push/pull elements to/from votes.
Then you sort on that votes_count field. If you also index it, queries will be faster.