Sort by reverse order mongoose - node.js

Hi, I am trying to return my query in backwards order from which it was created.
The docs are a little unclear on how to use the sort method:
http://mongoosejs.com/docs/api.html#types_array_MongooseArray.sort
Here is my schema:
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.Types.ObjectId;
let PostSchema = new Schema({
title : String,
description: String,
image : String,
tags : [String],
original_poster: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
date: {
type: Date,
default: new Date()
}
})
module.exports = mongoose.model('Post',PostSchema);
I have run,
db.posts.find().sort({date:-1}).pretty()
For example, if my model was a 'Post' model and my first post was 'hello world' and my second post was 'this is a post'. I would like to see:
['this is a post', 'hello world']
However, what I am actually seeing is ['hello world','this is a post']

Figured out the answer
in posts schema add:
date: {
type: Date,
default: Date.now
}
then db.posts.find().sort({date:-1}).pretty() will yield the posts sorted from most recent to least recent

You have to add a creation timestamp in your schema and sort by its key.
let PostSchema = new Schema({
title : String,
description: String,
date : Date, // Here is your date
image : String,
tags : [String],
original_poster: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
}
})
and when you insert a document, use:
date: new Date()

Related

How to create a dynamic nested mongoose document with the same schema on multiple levels

Before everyone tells me I can't call a const before initializing, I do know that.
But I think this is the simplest way to render the concept I have in mind, (where any subdocument within the replies array also has the same schema as the parent, and documents within the replies array of those subdocuments also having the same schema). I would really appreciate anyone's input.
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
var commentSchema = new mongoose.Schema({
content: String,
createdAt: {
type: Date,
default: Date.now
},
score: {
type: Number,
default: 1
},
username: {
type: String,
lowercase: true
},
parent: {
type: Schema.Types.ObjectId,
ref: 'comment'
},
replyingTo: String,
replies: [commentSchema]
});
module.exports = mongoose.model("comment", commentSchema);
Since a const can't be called before initialization, to fix this issue the parent schema should be called on the children array after initialization the code below:
commentSchema.add({ replies: [commentSchema] })
The final result should look like this:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const commentSchema = new mongoose.Schema({
content: String,
createdAt: {
type: Date,
default: Date.now
},
score: {
type: Number,
default: 1
},
username: {
type: String,
lowercase: true
},
parent: {
type: Schema.Types.ObjectId,
ref: 'comment'
},
replyingTo: String,
});
commentSchema.add({ replies: [commentSchema] })

Mongoose Populate Method is not populating value

I have two mongoose schema as following
var ServiceSubType = new Schema({
displaySubTypeName : String,
subTypeDescription : String,
status : { type: String, default: Constants.ACTIVE },
lastUpdatedOn : Date,
createdOn : { type: Date, default: Date.now }
} , { collection: 'SERVICE_SUB_TYPES' });
and
var ServiceType = new Schema({
displayName : String,
description : String,
status : { type: String, default: Constants.ACTIVE },
lastUpdatedOn : Date,
serviceSubTypeId : {type: Schema.Types.ObjectId, ref: 'ServiceSubType', index: true},
createdBy : { type: Schema.Types.ObjectId, ref: 'SystemUser', index: true },
createdOn : { type: Date, default: Date.now }
} , { collection: 'SERVICE_TYPES' });
I have populated Type Object as below
module.exports.addNewServiceType = function(serviceType_obj, callback) {
serviceType_obj.save(serviceType_obj,callback);
}
Now I am trying to populate ServiceSubType document and then at the same time trying to populate "serviceSubTypeId" of ServiceType object referenced to ServiceSubType created.
Here is my piece of code for the same purpose.
module.exports.addServiceSubType = function(serviceTypeObjId, serviceSubType_obj, callback) {
serviceSubType_obj.save(serviceSubType_obj, function (error, serviceSubType) {
});
serviceSchema.ServiceType.findById(serviceTypeObjId, function (err, serviceType) {
var opts = { path: 'serviceSubTypeId'};
serviceSchema.ServiceType.populate(serviceType, opts, function (err, user) {
console.log(serviceType);
});
}).exec(callback);
}
But it is not workign and not populating any value in Existing SubType object.
I admit my approach could be very wrong as I am very new in this technology. Appreciate any kind of help to run this piece of code as expected.
Thanks
Ajoy
I think your ref should be the same setting as the collection on the object
serviceSubTypeId : {
type: Schema.Types.ObjectId,
ref: 'SERVICE_SUB_TYPES', <==== your collection type goes here
index: true
},
Mongoose doesn't know anything about your JavaScript object types. Instead, it tracks things based on the collection name that you provide (or that it generates).
update based on comments below
I have some example code that I wrote a while back, and it looks like I'm letting Mongoose generate the collection name for me. However, I am supplying a name to the mogoose.model() call, when registering my type for a collection.
For example, I have a Post type and a User type. The Post contains an author which is a reference to the User.
It looks like this:
// post
// ----
var PostSchema = new mongoose.Schema({
date: {type: Date, required: true, default: Date.now},
title: {type: String, required: true},
content: {type: String, required: true},
author: {
type: SchemaTypes.ObjectId,
ref: "user"
},
comments: [CommentSchema]
});
var Post = mongoose.model("blog", PostSchema);
// user
// ----
var UserSchema = mongoose.Schema({
firstName: {type: String},
lastName: {type: String},
username: {type: String, required: true, index: {unique: true}},
email: {type: String, required: true},
password: {type: String, required: true},
url: {
type: mongoose.Schema.Types.ObjectId,
ref: "url"
}
});
User = mongoose.model("user", UserSchema);
In this example code, I'm setting the ref to "user" because I am registering my model as "user" down below, in the mongoose.model method call.
Are you registering your models using mongoose.model and supplying a name? if so, use that name.

Mongoose- How to reference embed document element?

I have users.js schema with a embeded document array pets. For each user, a user can have multiple pets(usually no more than 3 I would think).
For each pet, there would be a daily chart. So it would be many daily charts for a pet. I have read with embedded documents that each array element is indexed. In daily.js, how can I reference the pet it would belong to for the populate() function?
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
firstName: { type: String, required: true },
lastName: { type: String, required: true },
username: { type: String, required: true, unique: true },
location: String,
pets: [{ name: 'string', animalType: 'string'}], //could have more than one pet
created_at: Date,
updated_at: Date
});
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var dailySchema = new Schema({
tite: String,
_pet: { type: Number, ref: 'User.pet' }, // not sure how to reference name in user.pets[#] array
created_at: Date,
updated_at: Date
});
Quoting
Sorry to disappoint but that is an anti-pattern. Populate can't populate from another collection's subdocs - the reason why you're getting that error is that there's no model for boards.
So it may be not good patten to reference to embedded document. It could be better to separate pet from User as one schema
var PetSchema = new Schema ({
name: 'string',
animalType: 'string'
});
And the UserSchema and DailySchema will be
var userSchema = new Schema({
...
pets: [{ type: Schema.Types.ObjectId, ref: 'Pet' }], //could have more than one pet
});
var dailySchema = new Schema({
_pet: { type: Number, ref: 'Pet' }, // not sure how to reference name in user.pets[#] array
});

How to compare _id value of Models made using Mongoose?

I have the following two schemas of User and Critique and I've got the data persisted in MongoDB database:
var userSchema = mongoose.Schema({
critiques: [{ type: Schema.ObjectId, ref: 'Critique' }],
});
var User = mongoose.model("User", userSchema);
var critiqueSchema = mongoose.Schema({
author: {type: String, default: ''},
date: { type: Date, default: Date.now },
comment: { type: String, default: '' },
stars: { type: Number, default: 0 },
_user: { type: Schema.ObjectId, ref: 'User' }
});
var Critique = mongoose.model("Critique", critiqueSchema);
user.critiques[0]._id.equals(critique._id) is giving me undefined is not a function.
How to compare _id value of a User instance with the Critique instance?
The critiques field of your user object directly contains an array of ObjectIds, so it would just be:
user.critiques[0].equals(critique._id)
user.critiques would only contain full Critique objects if you chained a .populate('critiques') call in the find where you obtained user.

Adding an array inside my Mongoose schema

I have the following schema where I am trying to add an array of comments to my blog post schema, then inside the comments schema I need to add an array of pictures urls related to each specific comment. I've researched the web and found this link embedded documents to mongoose documentation, yet noticed that it is related to mongoose version 2.7 while we are currently at version 3.8. So was wondering if I am doing it right?, and if not can someone please help me by suggesting the best way for designing my blog post schema so that it includes the blog post array of comments as well as the array of pictures related to each comment. Thanks for your time and effort.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var pictures = new Schema({
picURL: String,
date: {
type: Date,
default: Date.now
}
});
var comments = new Schema({
subject: String,
body: String,
date: {
type: Date,
default: Date.now
},
pictures:[pictures]
});
var blogpost = new Schema({
title: String,
body: String,
date: {
type: Date,
default: Date.now
},
comments:[comments]
});
module.exports = mongoose.model('BlogPost', blogpost);
you have two common scenarios here how you would like to handle your information
Embedded document:
if you are likely to do more reads than writes it's recommended to follow this approach in this case your model could be like this:
var comments = new Schema({
subject: String,
body: String,
date: {
type: Date,
default: Date.now
},
pictures:[{
picURL: String,
date: {
type: Date,
default: Date.now
}
}]
});
and also your approach to me is ok and should run on 3.8 without potential issues.
Referenced document:
if you'll have more writes than reads you can different collections to split the information and make a reference to your objectId like:
var comments = new Schema({
subject: String,
body: String,
date: {
type: Date,
default: Date.now
},
pictures: [
{type: Schema.Types.ObjectId, ref: 'pictures'}
]
});
you'll need to separate each schema into it's own and delare a model for comments and images as well.
Either way both are valid if you ask me my personal preference is picking up embedded document approach.
EDIT:
this situation can be applied for N relationships between collections, I keep it simple for two relationships, but for you scenario could be like this:
var blogpost = new Schema({
title: String,
body: String,
date: {
type: Date,
default: Date.now
},
comments: [{
subject: String,
body: String,
date: {
type: Date,
default: Date.now
},
pictures:[{
picURL: String,
date: {
type: Date,
default: Date.now
}
}]
}]
});
referenced:
var blogpost = new Schema({
title: String,
body: String,
date: {
type: Date,
default: Date.now
},
comments:type: Schema.Types.ObjectId, ref: 'comments'}
});
hope that helps.

Resources