Inserting Object ID's into array in existing Mongoose schema with Node.js - javascript

I have an existing News articles section that I want to add categories to for more refined searching, my Schema's are as follows:
var ArticleSchema = new Schema({
title: String,
body: String,
author: {
type: Schema.Type.ObjectId,
ref: 'User',
required: true
},
image: String,
catagories: [{
type: Schema.Types.ObjectId, ref: 'Catagory'
}],
meta: {
created: {
type: Date,
'default': Date.now,
set: function(val) {
return undefined;
}
},
updated: {
type: Date,
'default': Date.now
}
}
});
ArticleSchema.statics.search = function (str, callback) {
var regexp = new RegExp(str, 'i');
return this.find({'$or': [{title: regexp}, {body: regexp}]}, callback);
};
module.exports = ArticleSchema;
var CatagorySchema = new mongoose.Schema({
name: { type: String, unique: true },
});
module.exports = CatagorySchema;
I want a user friendly input for selecting categories (don't even know what is best here, be it check-box's or a simple comma separated text input etc.). My question is what is the best practice for obtaining this kind of input and translating that into the Article Schema (providing the categories exist). If anyone could point me in the right direction it would be much appreciated. Thanks.

Keep the category names you want to search for in an array
{
categories: ["cat1", "cat2"]
}
then you can add an index to it and do a $in query. the current schema is not very good because you cannot look for the category in a single query but need to resolve all the "categories" links first.

Related

how to use find function in nodejs

const categorySchema = mongoose.Schema({
name: {
required: true,
type: String
}
});
const productSchema = mongoose.Schema({
name: {
type: String,
required: true
},
category: {
type: mongoose.Schema.Types.Mixed,
ref: "Category",
required: true
}
});
As you see above I have two models where I am using Category inside Product.
Now I want to fetch all the products by passing a particular Category or its id as _id which gets generated by default by mongodb.
Although I am achieving the desired result do this below:
const id = req.query.categoryId;//getting this from GET REQUEST
let tempProducts = [];
let products = await Product.find({});
products.forEach(product => {
if (product.category._id===id){
tempProducts.push(product);
}
});
It gets me what I want to achieve but still I want to know how to get it using "find" function. or this what I am doing is the only way.
Ok thank you all for your time. I found out the solution which is:
First of all below is my schema as Product which includes another schema named Category:
const productSchema = mongoose.Schema({
name: { type: String, required: true },
category: { type: mongoose.Schema.Types.Mixed, ref: "Category", required: true } });
And to get all the products related to a particular category,
We need to add our query inside quotes like this:
let tempProducts = [];
tempProducts = await Product.find({ "category._id": id});
This is how I achieved as I wanted and working as expected.

Adding documents to a Many to Many relationship in Mongoose (MongoDB)

I have a User collection and a Task Collection. Many Users can have many Tasks, and Many Tasks can have many Users. The proper way I believe in doing this is the following models:
var UserSchema = new Schema({
id: ObjectId,
username: { type: String, required: true },
assignments: [ {type : mongoose.Schema.ObjectId, ref : 'Assignment'} ]
});
var TaskSchema = new Schema({
id: ObjectId,
title: { type: String, default: '' },
information: { type: String, default: '' },
assignments: [ {type : mongoose.Schema.ObjectId, ref : 'Assignment'} ]
});
var AssignmentSchema = new Schema({
id: ObjectId,
isCompleted: { type: Boolean, default: false },
completionDate: { type: Date, default: null },
tasks: [ {type : mongoose.Schema.ObjectId, ref : 'Task'} ],
users: [ {type : mongoose.Schema.ObjectId, ref : 'User'} ]
});
If the above models are correct, how do you insert a Task with multiple user assignments? I understand that you would create the Task document first to get its ObjectId, but after that would you just insert all of the assignments into the Assignment collection (with their proper Task and User objectId's) and thats it? Or would I have to insert all of the assignments then edit each individual User and Task to insert the AssignmentId into their assignments property.
I am sure there is a stack over flow question like this already, but I have not been able to find one. Any help is appreciated!
I believe you have the answer in your question.
Create a new task, capture the task id. Then find or create a new user, edit or add the task’s id, capture the user id. Add the user id to the task. Repeat for additional users.
I believe this is what you say in the final part of your question?
I don’t see why this cannot work.

Mongoose/MongoDb ,how to validate an array of Ids against another model

I have 2 moongose Schema:
var Schema2 = new Schema({
creator : { type: String, ref: 'User'},
schema_name : [{ type: String}],
});
var Schema1 = new Schema({
creator : { type: String, ref: 'User'},
schema_ref : [{ type: String, ref: 'Schema2' }],
});
Would like to know which is the best practice when I create a new Schema1 check that every element of array schema_ref, have the same creator.
Because schema1 elements are added by client form and so i have to check that the schema_ref elements are owned by same User that send the form
You can try with either validator function, or with a simple 'save' middleware:
Schema1.pre('save', function(next) {
let owner;
for (let entry in this.schema_ref) {
if (!owner) {
owner = entry;
} else {
if (entry !== owner) {
return next(new Error("owner mismatch");
}
}
}
});
Also, your schema might not work as you expect it to, it looks like you actually need:
schema_ref: [{
type: {type: String},
ref: "User"
}]
Additionally, take a look at id-validator plugin, or some similar to that - it will, in addition to your validation, also check that all ref-type properties like this actually exist in the other (Users) collection.

Saving an array (of 'tags') to MongoDB using Mongoose

I'm playing around with Mongoose and I'm having trouble saving to an array. For example, I have an input field on a page for comma-separated tags. I grab these from req.body.tags, remove the white space, and split them by the commas to get an array of the tags. Now, how do I save this array back to my database? I'm guessing I've set up this part of the schema wrong but I'm not sure what's right, to be honest. The schema I have so far is:
var postSchema = mongoose.Schema({
title: String,
permalink: String,
content: String,
author: {
id: String,
name: String,
},
postDate: {
type: Date,
default: Date.now
},
});
If I was to save tags (from a post, for example) would it be best to have an array called tags and then each tag has a name (and id?)? If so, would I just add something like this (below) to the schema? The idea of adding tags to a post is so that I can display them (as links) on the post and be able to search the database for all posts with a certain tag. Is this the right way to go about it?
tags: [{
name: String,
id: String
}]
When posting to the new post route, I'm doing the following:
post = new Post(req.body);
tags = req.body.tags.replace(/\s/g,'').split(',');
// maybe post.tags = tags ?
post.save(function(err) {
if (!err) {
res.redirect('/posts');
} else {
...
}
});
This successfully saves all other fields submitted (title, author, content, etc.) but I'm not sure how I can save the newly-created tags back to the database. Any advice here would be very welcome as I'm new to this and keen to keep learning. Thanks!
If you really want your "tags" array to have a name field and a generated _id field then define another schema and embed it:
var tagSchema = mongoose.Schema({
name: String
});
var postSchema = mongoose.Schema({
title: String,
permalink: String,
content: String,
author: {
id: String,
name: String,
},
postDate: {
type: Date,
default: Date.now
},
tags: [tagSchema]
});
Then manipulate the input to the correct structure before you create the Post object:
req.body.tags = req.body.tags.replace(/\s/''/g).split(",").map(function(tag) {
return { "name": tag };
});
var post = new Post(req.body);
Or just leave it as an array of plain strings:
var postSchema = mongoose.Schema({
title: String,
permalink: String,
content: String,
author: {
id: String,
name: String,
},
postDate: {
type: Date,
default: Date.now
},
tags: [String]
});
And don't worry about mapping the object property:
req.body.tags = req.body.tags.replace(/\s/''/g).split(",");
var post = new Post(req.body);

Complex Schema with subdocument

UPDATE:
Currently i have this schema:
var Schema = new schema ({
name: String,
type: { type: String },
date: { type: Date },
descrip: String,
});
But i use this schema for generate 2 documents:
THE A TYPE ({
name: 'A',
type: 'TYPE_B',
date: { type: Date },
descrip: 'DESC_A',
});
THE B TYPE ({
name: 'B',
type: 'TYPE_B',
date: { type: Date },
descrip: 'DESC_B',
});
The name, type and descrip are always the same in the A and B types, the only thing that changes is the date field, so i was thinking, how can i improve this? How can i insert several dates in the same schema, instead of create always an document with the same name, type and descrip values?
So i am trying to create a schema inside other schema, but i dont know if this is possible, it is?
I was trying like this:
var mainSchema = new schema ({
name: String,
type: { type: String },
date: [ dateSchema ],
descrip: String,
});
var dateSchema = new Schema ({
date: {
type: Date
}
});
What i want, is create two mainSchema, type a and type b, and insert dates inside...
Am i doing this right? How can i achieve my goals?
I am searching for a full answer with good explanation, that's why the bounty. I don't accept +/- answers.
To create a record with multiple dates you can use array of Dates.
var mainSchema = new schema ({
name: String,
type: { type: String },
dates: [Date],
descrip: String,
});
The documents will be as follows
THE A TYPE ({
name: 'A',
type: 'TYPE_B',
dates: ["2014-01-22T14:56:59.301Z","2015-01-22T14:56:59.301Z"],
descrip: 'DESC_A',
});
THE B TYPE ({
name: 'B',
type: 'TYPE_B',
dates: ["2015-01-22T14:56:59.301Z","2014-01-22T14:56:59.301Z"],
descrip: 'DESC_B',
});
Reference: http://mongoosejs.com/docs/schematypes.html
For saving the document you can use like.
exports.save = function(req,res){
var test = new newSchema; // new object for newSchema domain.
test.name= req.body.name;
test.type= req.body.type;
test.desc= req.body.desc;
if(req.body.date){
req.body.forEach(function(date){ // For every element of date from client.
test.date.push(date) // This pushes each and every date given from the client into the date array.
})
}
test.save(function (saveErr, saved) { // Saves the new document into db.
if (saveErr) {
console.log(saveErr)
return;
}
res.status(HttpStatus.OK).json(saved);
});
};
For Update you can use like.
exports.update = function(req,res){
newSchema.findOne({name: 'B'},function(err,test){ // Finds a record with name:'B' and stores in test the same can be done for name:'A'.
if(test){
if(req.body.date){
req.body.forEach(function(date){
test.date.push(date) // pushes dates into existing test document (update).
})
}
test.save(function (saveErr, saved) { // Saves (updates) the document.
if (saveErr) {
console.log(saveErr)
return;
}
res.status(HttpStatus.OK).json(saved);
});
}
});
};
Hope this helps.
If your code write down in one section it will look like this
var mainSchema = new schema ({
name: String,
type: { type: String },
date: [ {date: { type: Date } } ],
descrip: String,
});
I think that's wrong. Also you don't need to creat new schema like this, you can use just {} in your code http://www.w3schools.com/js/js_objects.asp . I think correct code must look like this:
var mainSchema = {
name: string,
date: {},
descrip: string
}
And then push into 'date' your dates. Good luck.
======================== HOW TO USE =======================
Sorry for delay. You can use mainSchema.date.date_1 = date; or you can chang date: {}, to date: [], and use mainSchema.date.push(date) depend on your needs.

Categories

Resources