I'm getting [] res in mongoose find by {parentId: cat._id}
var ObjectId = Schema.ObjectId;
var CategorySchema = new Schema({
name: String,
slug: String,
parentId: {type: ObjectId, required: false},
ancestors: {
type: [{
_id: ObjectId,
name: String,
slug: String
}], required: false
}
});
CategorySchema.statics.getNested = function(parentSlug,cb){
this.findOne({slug:parentSlug},function(err,cat){
if (err) {
cb(err);
} else {
this.find({parentId: cat._id},function(err, cats){
console.log(cats);
if (err){
cb(err);
} else {
cb(null,cats);
}
});
}
});
};
I tried {parentId: ObjectId(cat._id)} but this did not work too // ObjectId(cat._id) -> undefined
How do I search mongoose by _id?
UPDATE!
The query
Category.find({parentId:'5634eeb38a33a59c1dffa6ee'}, function(err,res){
console.log(res);
});
is working fine but how?
It should be {parentId: mongoose.Types.ObjectId(cat._id)}
You have incorrect ObjectId type in your schema, you should use Schema.Types.ObjectId instead of Schema.ObjectId because they return different values:
http://mongoosejs.com/docs/api.html#schema-objectid-js
http://mongoosejs.com/docs/api.html#types-objectid-js
If you replace your ObjectId definition to the following
var ObjectId = Schema.Types.ObjectId;
your code should work correctly.
Related
I am new in mongo and node and I am facing a problem in filtering.
I have a customer schema and wallet schema. When I am inserting a new
customer it is populating a wallet for that customer. Schema of this
two model is below.
Customer.model.js
var Schema = mongoose.Schema;
const Genders = Object.freeze({
Male: 'male',
Female: 'female',
Other: 'other',
});
var CustomerSchema = new Schema({
reg_date: { type: Date, default: Date.now },
first_name: String,
last_name: String,
gender: {
type: String,
enum: Object.values(Genders),
},
wallet_balance: { type: Number, default: 0 },
status:{type:Boolean,default:true},
wallet:{type:mongoose.Schema.Types.ObjectId,ref:'Wallet'},
customer_rank: String
});
module.exports = mongoose.model('Customer', CustomerSchema);
Wallet.model.js
var Schema = mongoose.Schema;
var TransactionSchema = new Schema({
reason: String,
deposit_by: Number,
transaction_type: String,
transacted_balnace:Number
})
var WalletSchema = new Schema({
user_id:String,
transaction_log: [TransactionSchema],
balance: { type: Number, default: 0 },
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now }
});
WalletSchema.plugin(uniqueValidator);
module.exports = mongoose.model('Wallet', WalletSchema);
I want to get customer details on basis of reason.
So, the code is below.
CustomerModel.find({}, { "password": 0 }).populate({
path: 'wallet',
match: { reason: { $in: [ "service charge" ] } },
select: 'transaction_log'
}).exec().then(data => {
if (data) {
res.status(200).send({ status: true, data: data })
}
else {
res.send({ status: false, data: [] })
}
})
It is not returning the wallet, But if I remove the match property it
is working fine.It will be very helpful if I get a solution. Thanks
in advance.
I want to execute a query for getting all "group" documents that have the userId in there array of users.
I've tried several different ways of query, but I always get an empty array.
What am I doing wrong?
group.js
let mongoose = require('mongoose');
const Group = mongoose.Schema({
name: {
type: String,
required: true
},
users: [{
userId: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'users',
required: true
},
userType: {
type: String,
required: true
},
userStatus: {
type: String,
required: true
}
}]
})
module.exports = mongoose.model('group', Group);
groupController.js
exports.getUserGroups = function (req, res) {
Group.find({
"users.userid": "req.user._id"
}, function (err, groups) {
if (err)
res.send(err)
res.json(groups);
});
}
Field names are case-sensitive, so "users.userid" should be "users.userId" instead:
Group.find({
"users.userId": "req.user._id"
}, ...
You can try using $in operator
db.collection.find({
"users.userId": {
$in: [
req.user._id
]
}
})
(Basic library CRUD application)
I am trying to create a document containing some global data about a given book, and then within a User document, add the ObjectId of the newly-created book to an array containing all books belonging to that user.
I have three data models in my application:
var userSchema = new mongoose.Schema({
name: String,
password: String,
email: String,
books: [BookInstanceSchema],
shelves: [String]
});
var bookSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
author: {
type: String,
required: true
},
description: String,
pageCount: Number,
ISBN: String,
googleID: String,
thumbnail: String,
publisher: String,
published: String,
});
var BookInstanceSchema = new mongoose.Schema({
bookId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Book'
},
userReview: String,
userRating: {
type: Number,
get: v => Math.round(v),
set: v => Math.round(v),
min: 0,
max: 4,
default: 0
},
shelf: String
});
The User model contains a nested array of BookInstances, which contain user-specific data such as ratings or reviews for a given book. A bookInstance in turn contains a reference to the global data for a book, to avoid duplicating data that isn't specific to any user.
What I'm trying to do is first save the global data for a book (thus generating an _id), and when done, save a bookInstance containing that _id in a given user's array of books:
router.post('/save/:id', function(req, res) {
var url = encodeurl('https://www.googleapis.com/books/v1/volumes/' + req.params.id);
request(url, function(err, response, data) {
parsedData = JSON.parse(data);
var newBook = {
title: parsedData.volumeInfo.title,
author: parsedData.volumeInfo.authors[0],
description: parsedData.volumeInfo.description,
pageCount: parsedData.volumeInfo.pageCount,
ISBN: parsedData.volumeInfo.description,
googleID: parsedData.id,
publisher: parsedData.volumeInfo.publisher,
published: parsedData.volumeInfo.publishedDate,
thumbnail: parsedData.volumeInfo.imageLinks.thumbnail
};
Book.create(newBook, function(err, newBook) {
if (err) {
console.log(err);
}
else {
console.log(newBook._id);
console.log(mongoose.Types.ObjectId.isValid(newbook._id));
User.findByIdAndUpdate(req.session.id, {
$push: {
"books": {
bookId: newBook._id,
userRating: 0,
userReview: ''
}
}
},
{
upsert: true
},
function(err, data){
if(err) {
console.log(err);
}
else {
res.redirect('/');
}
});
}
});
});
});
I'm getting the error:
message: 'Cast to ObjectId failed for value "hjhHy8TcIQ6lOjHRJZ12LPU1B0AySrS0" at path "_id" for model "User"',
name: 'CastError',
stringValue: '"hjhHy8TcIQ6lOjHRJZ12LPU1B0AySrS0"',
kind: 'ObjectId',
value: 'hjhHy8TcIQ6lOjHRJZ12LPU1B0AySrS0',
path: '_id',
reason: undefined,
Every time, the value in the error (in this case, jhHy8T...) is different than the newBook._id I'm attempting to push into the array:
console.log(newBook._id); // 5a120272d4201d4399e465f5
console.log(mongoose.Types.ObjectId.isValid(newBook._id)); // true
It seems to me something is wrong with my User update statement:
User.findByIdAndUpdate(req.session.id, {
$push: {
"books": {
bookId: newBook._id,
userRating: 0,
userReview: ''
}
}...
Any help or suggestions on how to better organize my data are appreciated. Thanks!
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);
});
});
Taking the example from here
http://mongoosejs.com/docs/populate.html
If I try to tweak it so that 'fans' also contains a rating
var db = require('houselib/db');
var Schema = db.Schema;
var mongoose = db.mongoose;
var PersonSchema = new Schema({
name : String
, age : Number
, stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});
var StorySchema = new Schema({
_creator : { type: Schema.ObjectId, ref: 'Person' }
, title : String
, fans : [{ type: Schema.ObjectId, ref: 'Person', rating: Number}]
});
var Story = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);
var aaron = new Person({ name: 'Aaron', age: 100 });
aaron.save(function (err) {
if (err) throw err;
var story1 = new Story({
title: "A man who cooked Nintendo"
, _creator: aaron._id
, fans: [{type: aaron._id, rating: 4}]
});
story1.save(function (err) {
if (err) throw err;
Story
.find({ _creator: aaron._id })
.populate('_creator') // <-- not really necessary
.run(function (err, stories) {
if (err) throw err;
console.log('The stories JSON is an array: ', stories);
})
});
})
I get the following error
CastError: Cast to undefined failed for value "[object Object]"
The documentation says that manual linking is preferred over DBRef
http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-SimpleDirect%2FManualLinking
story.fans is an array of objectids. objectids do cannot have ratings. You need to add the rating to the Person schema instead of the story schema.
var PersonSchema = new Schema({
name : String
, age : Number
, rating: Number
, stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});