Mongoose Populate() replaces array with object - mongodb

My schema looks like this
Schema()
export class User {
#Prop({ required: true })
name: string;
#Prop({ required: true })
email: string;
#Prop({ type: mongoose.Types.ObjectId, ref: 'Course' })
courses: Course[];
}
#Schema()
export class Course {
#Prop()
name: string;
#Prop([{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }])
participants: User[];
#Prop([{ type: mongoose.Schema.Types.ObjectId, ref: 'Lesson' }])
lessons: Lesson[];
}
what i'm trying to achieve is to get an array of courses that a specific user is signed up for. The way Im trying to do this looks like this.
async findUserCoursesByUserEmail(email) {
const user = await this.userModel
.findOne({ email })
.populate('courses')
.exec();
return user.courses;
}
But in response i get an object like this, but it should be an array.
{
"lessons": [],
"participants": [
"6157a5ba06afba420464fb65"
],
"_id": "6157a5ac06afba420464fb61",
"name": "Matematyka Rozszrzona",
"__v": 1
}
The user object looks like this.
{
"_id": "6157a5ba06afba420464fb65",
"name": "Jacob",
"email": "test#test.pl",
"courses": [
"6157a5ac06afba420464fb61",
"6158d64891be3f50f85bef0a"
],
"__v": 2
}
Any help would be highly appreciated cause i'm stuck for quite some time with this one.

as per nestjs/mongodb docs change the:
#Prop({ type: mongoose.Types.ObjectId, ref: 'Course' })
courses: Course[];
on your UserSchema to:
#Prop({ type: [{mongoose.Types.ObjectId, ref: 'Course'}] })
courses: Course[];

Related

MongoDB Find by id of object inside array

I have the following Model-Structure
[{
"orders":
[{
"_id": "5f24839133759d4859ef5233",
"owner": {
"_id": "5f17fdb08145508a94fc1bda"
},
}]
},
...
]
I want to get all objects that have an order with a specific owner id.
I tried find({"orders.owner._id": "5f17fdb08145508a94fc1bda" }) which just returns an empty array.
Tried many other variations also without dot-notation, but nothing worked.
I'm using mongoose to query the MongoDB database.
EDIT:
These are the Mongoose Schemata used for the collection:
const orderPoolShema:mongoose.Schema = new mongoose.Schema({
orders: [{type: mongoose.Types.ObjectId, ref: 'Order' }]
},
{
timestamps: true
});
const orderShema:mongoose.Schema = new mongoose.Schema({
owner: {type: mongoose.Types.ObjectId, ref: 'User', require },
},
{
timestamps: true
});
const userShema:mongoose.Schema = new mongoose.Schema({
firstname: {type: String, required: true},
lastname: {type: String, required: true}
},
{
timestamps: true
});
And this is my Node.js code:
OrderPool.find({"orders.owner._id": mongoose.Types.ObjectId("5f17fdb08145508a94fc1bda") })
.then((orderPools:any) => {
res.send(orderPools);
}).catch((err:any) => {
res.status(500).send({
message: err.message || "Some error occurred while retrieving orderPools."
});
});
Any ideas on how to get that query to work are very appreciated.
Thanks for your help.

How to use full text search in referenced documents?

I know this was asked before, but I can't find an answer that works for me.
I have some documents, which have reference to another document, like users and orders:
Users model:
import mongoose from '../database/index.js';
import mongoosePaginate from 'mongoose-paginate-v2';
const UsersSchema = new mongoose.Schema({
user_id: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
firstName: {
type: String,
default: '',
},
lastName: {
type: String,
default: '',
},
orders: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Orders',
},
],
recipients: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Recipients',
},
],
createdAt: {
type: Date,
default: Date.now,
required: true,
select: false,
},
});
UsersSchema.index({ email: 'text', firstName: 'text', lastName: 'text' });
UsersSchema.plugin(mongoosePaginate);
UsersSchema.set('toJSON', {
virtuals: true,
});
const Users = mongoose.model('Users', UsersSchema);
export default Users;
Orders model:
import mongoose from '../database/index.js';
import mongoosePaginate from 'mongoose-paginate-v2';
import Users from './users.js';
import OrderStatus from '../enums/OrderStatusEnum.js';
const OrdersSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Users',
required: true,
},
total: {
type: Number,
required: true,
},
status: {
type: String,
enum: OrderStatus.values(),
required: true,
default: OrderStatus.CREATED,
},
payment: {
type: mongoose.Schema.Types.ObjectId,
ref: 'PaymentMethods',
required: true,
},
shortId: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
required: true,
},
});
OrdersSchema.index({ shortId: 'text' });
OrdersSchema.plugin(mongoosePaginate);
OrdersSchema.pre('remove', function (next) {
Users.update({ orders: this._id }, { $pull: { orders: this._id } }).exec();
next();
});
OrdersSchema.set('toJSON', {
virtuals: true,
});
const Orders = mongoose.model('Orders', OrdersSchema);
export default Orders;
I can use the $text search to query full text search from orders or users:
const orders = await Orders.paginate(
{ $text: { $search: query.filter.q } },
query.options
);
return orders;
But this will only make a search on the orders index. If, i.e., I would like to search for the order of the user whose first name is Joe, how can I make it also search in the user.firstName field, since this is a reference from Orders to Users?
I know I can't populate the fields and then search on all model, but I'm not sure how to achieve what I'm looking for.
Thanks in advance
Because full text search query must be the first stage in the aggregation pipeline, it is not currently possible to perform full text search in two collections as part of the same query.
You'll need to reorganize your data.
my requirement was to find orders of users matched with their names, partial or full-text search. the user id is ref into orders doc.
from mongodb playground:
https://mongoplayground.net/p/ASOSFvfURXW
db.inventory.aggregate([
{
"$lookup": {
"from": "orders",
"localField": "orderId",
"foreignField": "_id",
"as": "order_docs"
}
},
{
"$unwind": "$order_docs"
},
{
"$match": {
"order_docs.item": {
"$regex": "pec",
"$options": "i"
}
}
}
])

find one with multiple conditions mongoose mongodb

I am trying to obtain data from mongodb. This is the scenario. Each user has a following property(array) that takes an array of users id.
The user model is as below
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
followers: [{ type: mongoose.Types.ObjectId, ref: "user" }],
following: [{ type: mongoose.Types.ObjectId, ref: "user" }],
});
in simple terms. I need to use two conditions where postedBy: { $in: req.user.following }, or postedBy:req.user._id
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
photo: {
type: String,
required: true,
},
likes: [{ type: mongoose.Schema.ObjectId, ref: "user" }],
comments: [
{
text: String,
postedBy: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
},
],
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
},
});
I have not figured out the second condition to add in the code below.
router.get("/getSubPost", requireLogin, async (req, res) => {
try {
const result = await Post.find({
postedBy: { $in: req.user.following },
})
.populate("postedBy", "-password")
.populate("comments.postedBy", "_id name");
res.json({ result });
} catch (error) {
console.log(error);
}
});

Collection referencing multiple collections

I want to have a collection with multiple fields referencing multiple collections, something like it:
var comboSchema = new Schema({
oneId: { type: Schema.Types.ObjectId, ref: "One" },
twoId: { type: Schema.Types.ObjectId, ref: "Two" },
threeId: { type: Schema.Types.ObjectId, ref: "Three" },
components: {
id: {type: Schema.Types.ObjectId, ref: "Component"},
amount: {type: Number}
}
}
I know I can use $lookup and aggregate to get data, but it looks like it works only on a single field in a collection?
Any help? Thank you! :-)
This is a model sample using the ref, the ref key in the object will take the name of the model in which you are referencing
const mongoose = require('mongoose');
const postSchema = mongoose.Schema({
text: {
type: String,
required: 1
},
mediatype: {
type: String,
required: 1
},
media: {
type: String,
required: true
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
},
likes: {
type: [{
userid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
}
}]
},
comments: {
type: [{
userid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
},
comment: String
}]
},
}, {
timestamps: true
})
const Post = mongoose.model('post', postSchema)
module.exports = Post
so you can then populate it like Post.find().populate('user')

Mongo find users that are not blocked

I have a db in Mongo with users. I want to retrieve all user that are not in my blocked Array. tried to filter through the results by retrieving All documents but I wonder if that's the most efficient way. Is there a way in Mongo to find all users that are not part of an array?
Here is my User model:
var UserSchema = mongoose.Schema({
username: {
type: String,
index:true,
unique: true
},
password: {
type: String
},
email: {
type: String,
unique: true
},
name: {
type: String
},
latitude:{
type: String
},
longitude:{
type: String
},
blocked:{
type: Array
},
friends:{
type: Array
},
photo:{
type: String
},
identity:{
type: String
},
age:{
type: String
},
herefor:{
type: String
},
conns:{
type: Number
},
status:{
type: String
}
}, { collection: 'users' });
You are probably looking for something like the following, assuming your blocked users array contains usernames and your mongoose model is called UserModel:
var blockedUsernamesArray = [];
UserModel.find({ username: { $nin: blockedUsernamesArray } }, function(err, docs) {
// Handle result
})