Find documents by autopopulated fields Mongodb - mongodb-query

here is my Schema.
const PromocodeSchema = new Schema({
user_id: { type: Schema.Types.ObjectId, ref: 'User', autopopulate: true },
promocode_name: { type: String },
promocode: { type: String },
)}
How can I find this document by first_name of user_id which is a auto populated field
"_id": "60531cd4a0db4a3f09c2dcb8",
"user_id": {
"first_name": "ART",
"last_name": "INMOVE",
"domain": "test2.com",
},
"promocode_name": "Art10",
"promocode": "ART10",
"discount_percentage": 10,

Related

TypeORM find method returns non-sense data

So I'm trying to make a simple GET route to get some orders from my DB.
My Order entity looks like that :
#Entity('orders')
export class Order extends BaseEntity {
#PrimaryGeneratedColumn()
id: number;
#CreateDateColumn()
createdAt: string;
#Column()
price: number;
#ManyToOne(() => Status, (status) => status.order)
status: Status;
#OneToMany(
() => OrderHaveProducts,
(OrderHaveProducts) => OrderHaveProducts.product
)
products: OrderHaveProducts[];
}
And I'm trying to get with each order, the products ordered, that are stored in another table and I'm using the entity called OrderHaveProducts that looks like that:
#Entity('orders_have_products')
export class OrderHaveProducts extends BaseEntity {
#PrimaryGeneratedColumn()
id: number;
#ManyToOne(() => Order, (order) => order.products)
#JoinColumn({ name: 'order_id' })
order: Order;
#ManyToOne(() => Product, (product) => product.orders)
#JoinColumn({ name: 'product_id' })
product: Product;
#ManyToMany(() => Ingredient)
#JoinTable({
name: 'ordered_products_have_removed_ingredients',
joinColumn: {
name: 'ordered_product_id',
referencedColumnName: 'id',
},
inverseJoinColumn: {
name: 'ingredient_id',
referencedColumnName: 'id',
},
})
removed_ingredients: Ingredient[];
#ManyToMany(() => Ingredient)
#JoinTable({
name: 'custom_products_have_ingredients',
joinColumn: {
name: 'ordered_product_id',
referencedColumnName: 'id',
},
inverseJoinColumn: {
name: 'ingredient_id',
referencedColumnName: 'id',
},
})
added_ingredients: Ingredient[];
}
In my DB, in my OrderHaveProducts table I have this data:
OrderHaveProducts table in PHPMyAdmin
Where product_id 4 is a pizza called "BBQ" and product_id 2 is a pizza called "Margarita".
The issue I have is that when I execute this code:
const orders: Order[] = await Order.find({
relations: {
status: true,
products: {
product: true,
removed_ingredients: true,
added_ingredients: true,
},
},
});
The JSON returned is this:
{
"status": 200,
"data": {
"orders": [
{
"id": 1,
"createdAt": "2023-02-11T22:50:18.721Z",
"price": 27,
"status": {
"id": 4,
"name": "finished"
},
"products": []
},
{
"id": 2,
"createdAt": "2023-02-11T22:50:18.735Z",
"price": 15,
"status": {
"id": 4,
"name": "finished"
},
"products": [
{
"id": 1,
"product": {
"id": 2,
"name": "Margarita",
"price": 12,
"picture: "https://medias.delarte.fr/media/sys_master/images/hdb/h49/8875895488542.png"
},
"removed_ingredients": [
{
"id": 5,
"name": "mozzarella",
"stock": 50
}
],
"added_ingredients": []
}
]
}
]
}
}
Where we can clearly see that my first order doesn't have any product (when in DB we see that it has 2) and the second order has 1 product (as expected) but it's not the good product.
I tried to only get the data from the OrderHaveProducts table and I successfuly get the data I see in my DB:
{
"status": 200,
"data": {
"orders": [
{
"id": 1,
"order": {
"id": 1,
"createdAt": "2023-02-11T22:50:18.721Z",
"price": 27
},
"product": {
"id": 2,
"name": "Margarita",
"price": 12,
"picture": "https://medias.delarte.fr/media/sys_master/images/hdb/h49/8875895488542.png"
}
},
{
"id": 2,
"order": {
"id": 1,
"createdAt": "2023-02-11T22:50:18.721Z",
"price": 27
},
"product": {
"id": 4,
"name": "BBQ",
"price": 15,
"picture": "https://cdn.shopify.com/s/files/1/0508/2179/1903/articles/25-Comment_cuire_pizza_barbecue_1500x.jpg?v=1619600472"
}
},
{
"id": 3,
"order": {
"id": 2,
"createdAt": "2023-02-11T22:50:18.735Z",
"price": 15
},
"product": {
"id": 4,
"name": "BBQ",
"price": 15,
"picture": "https://cdn.shopify.com/s/files/1/0508/2179/1903/articles/25-Comment_cuire_pizza_barbecue_1500x.jpg?v=1619600472"
}
}
]
}
}
If anyone can tell me what is going on with all of this it would be incredible, thanks !
I finally found the issue (issues ?). I actually had wrongly set up my relations between my Order entity and OrderHasProducts.
So in my Order entity my relation was like that:
#OneToMany(
() => OrderHaveProducts,
(OrderHaveProducts) => OrderHaveProducts.product // <-- Here I linked the entity to the product, when I should have linked it to order
)
products: OrderHaveProducts[];
So the good relation should have been this:
#OneToMany(
() => OrderHaveProducts,
(OrderHaveProducts) => OrderHaveProducts.order
)
products: OrderHaveProducts[];
And the second issue was coming from the OrderHaveProducts entity where I, another time, miss linked the entities, so I had this :
#ManyToOne(() => Order, (order) => order.products)
#JoinColumn({ name: 'order_id' })
order: Order;
#ManyToOne(() => Product, (product) => product.orders)
#JoinColumn({ name: 'product_id' })
product: Product;
When the good relation should have been this:
#Column()
order_id: number;
#Column()
product_id: number;
#ManyToOne(() => Order, (order) => order.id)
#JoinColumn({ name: 'order_id' })
order: Order;
#ManyToOne(() => Product, (product) => product.id)
#JoinColumn({ name: 'product_id' })
product: Product;
So now I successfuly get the good data.
Hope it can help anyone in the future ;)

AJV validate string against an array of objects

I'm trying to use AJV to check that parent_IDs on child objects are valid IDs on the parent objects.
I can get it to work by validating against a single parent object like this:
const ajv = new Ajv({
$data: true,
allErrors: true,
verbose: true,
});
addFormats(ajv, ["uuid"]);
const schema = {
type: "object",
properties: {
parentObjects: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string", format: "uuid" },
name: { type: "string" },
},
required: ["id", "name"],
},
},
childObjects: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string", format: "uuid" },
name: { type: "string" },
parent_id: {
type: "string",
format: "uuid",
const: { $data: "/parentObjects/0/id" },
},
},
required: ["id", "name", "parent_id"],
},
},
},
};
const data = {
parentObjects: [
{
id: "3bc169ba-99c4-448a-8c22-5c2593ccc9ee",
name: "larry",
},
{
id: "d2e92e51-cb56-4451-bf8b-349d82fde107",
name: "curly",
},
{
id: "b99f61f5-f417-4129-9315-ab049d2b618d",
name: "moe",
},
],
childObjects: [
{
id: "bd86603f-fb1d-4075-94f5-619337d43b98",
name: "stan",
parent_id: "3bc169ba-99c4-448a-8c22-5c2593ccc9ee",
},
{
id: "c2b80273-bf2b-4898-a44b-a758a436cb37",
name: "oliver",
parent_id: "e13b7c34-6cb5-4ef4-83a7-7d11c2bd4a1f",
},
],
};
ajv.validate(schema, data);
console.log(ajv.errors);
};
But what I need is to be able to validate against all parent objects, something like:
childObjects: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string", format: "uuid" },
name: { type: "string" },
parent_id: {
type: "string",
format: "uuid",
enum: { $data: "/parentObjects/*/id" },
},
},
required: ["id", "name", "parent_id"],
},
},
I can't find a way to use a wildcard for the array index "/parentObjects/*/id" or a way to iterate through all of the parentObjects. I could map the parentObject IDs outside of AJV, create another array and validate against that, but it seems like overkill - I feel like I'm missing something simple in AJV which would allow me to do this without generating a new array.

How can i make nested schema for a course with sub-topic and every sub-topic having Sub-topic Name, time duration, video URL in Sanity.io?

**
I don't Know how to make this please help if possible or tell me where can i find some similar Schema
I want to create video lecturer website
with topic, sub-topic, time-duration
**
export default {
name: "Courses",
title: "Property",
type: "document",
fields: [
{
title: "Movie",
name: "movie",
type: "document",
fields: [
{
title: "Title",
name: "title",
type: "string",
},
{
title: "Poster",
name: "poster",
type: "image",
},
{
title: "Sub-Topic",
name: "directors",
type: "array",
of: [{ type: "string" }],
},
{
title: "Video URL",
name: "imageUrl",
type: "url",
},
{
title: "Rich text example",
name: "myRichTextExample",
type: "array",
of: [{ type: "block" }],
},
],
},

How to display list of data in a column in iview table

In iview table, my colums:
export default {
data () {
return {
columns5: [
{
title: 'CNAME',
key: 'name',
sortable: true
},
{
title: 'nodes',
key: 'nodes',
},
{
title: 'domains',
key: 'domains',
},
{
title: 'desc',
key: 'desc'
},
{
title: 'duetime',
key: 'duetime',
sortable: true
},
{
title: 'ctime',
key: 'ctime',
sortable: true
},
{
title: 'uptime',
key: 'uptime',
sortable: true
},
],
the data:
anames:[
{
"id": 1,
"nodes": [
{
"id": 1,
"main_ip": "10.10.10.10",
"ips": "10.10.10.10\r\n10.10.10.11",
"req_secret": null,
"ctime": "2021-05-17T09:41:53.159131+08:00",
"uptime": "2021-05-17T10:31:47.886033+08:00",
"cname": 1
}
],
"domains": [
{
"id": 1,
"domain_name": "baidu.com",
"ctime": "2021-05-17T16:19:18.097807+08:00",
"uptime": "2021-05-17T16:19:18.097955+08:00",
"cname": 1
}
],
"name": "masdcom",
"desc": "",
"desc_en": null,
"is_active": true,
"duetime": "2021-06-17T19:40:00+08:00",
"ctime": "2021-05-17T09:13:57.019125+08:00",
"uptime": "2021-05-17T19:42:23.025122+08:00",
"user": 2
}
]
You see my nodes and domains,in data anames they are list, not just key-value, how can I display domain's domain_name and node's main_ip?
In there can not use domains.domain_name and nodes.main_ip.
I want nodes display all node's main_ip and domains display all domain's domain_name. what should I do in columns?
domains and nodes is array, you forgot about indexes
domains[0].domain_name and nodes[0].main_ip

Mongoose Schema - How to add an order attribute for sorting

I am currently building a web application where you can create setlists (arrays) with an array of lyric objectId's inside, that you can then sort / order into how you want it. So if you would like the 3rd list item to become the first, then you simply drag and drop it to the first line.
I now have a problem in my mongoose schema. I am looking for a way to implement an order attribute or something that would allow me to add a order value such as 0 or 1 depending on the position of the lyrics. Does any of you know how to best implement such order?
Here is a copy of my schema. Currently lyrics is an array of lyric objectId's. But in there i would need an "Order" as well, so that i can sort the array according to the order value.
const mongoose = require("mongoose");
const SetlistSchema = new mongoose.Schema({
setlistName: { type: String, required: true },
lastEdited: { type: Date },
createdAt: { type: Date, default: Date.now },
lyrics: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Lyric'
}],
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});
module.exports = mongoose.model("Setlist", SetlistSchema);
Here is the Lyrics schema.
const mongoose = require("mongoose");
const LyricSchema = new mongoose.Schema({
lyricName: { type: String, required: true },
lyricContent: { type: String, required: true },
lastEdited: { type: Date },
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});
module.exports = mongoose.model("Lyric", LyricSchema);
If adding an order number isn't the best practice, what can you then recommend as a way of keeping track of which order the user would like the lyrics to show up?
You can use aggregation framework to sort lyrics by order field. You first need to add a sort field with Number type.
Setlist.aggregate([
{
$unwind: "$lyrics"
},
{
$lookup: {
from: "lyrics", // MUST be the PHYSICAL collection name
localField: "lyrics",
foreignField: "_id",
as: "lyrics"
}
},
{
$sort: {
"lyrics.order": 1
}
},
{
"$group": {
"_id": "$_id",
"lyrics": {
"$push": "$lyrics"
},
"allFields": {
"$first": "$$ROOT"
}
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
"$allFields",
{
"lyrics": "$lyrics"
}
]
}
}
}
])
Playground
Sample documents:
db={
"lists": [
{
"_id": ObjectId("5a934e000102030405000000"),
"setlistName": "list1",
"lastEdited": ISODate("2020-03-18T23:11:56.443+03:00"),
"createdAt": ISODate("2020-03-15T23:11:56.443+03:00"),
"lyrics": [
ObjectId("6a934e000102030405000000"),
ObjectId("6a934e000102030405000001"),
ObjectId("6a934e000102030405000002")
]
},
{
"_id": ObjectId("5a934e000102030405000001"),
"setlistName": "list2",
"lastEdited": ISODate("2020-03-11T23:11:56.443+03:00"),
"createdAt": ISODate("2020-03-11T23:11:56.443+03:00"),
"lyrics": [
ObjectId("6a934e000102030405000003"),
ObjectId("6a934e000102030405000004")
]
}
],
"lyrics": [
{
"_id": ObjectId("6a934e000102030405000000"),
"name": "Lyric 1",
"order": 3
},
{
"_id": ObjectId("6a934e000102030405000001"),
"name": "Lyric 2",
"order": 1
},
{
"_id": ObjectId("6a934e000102030405000002"),
"name": "Lyric 3",
"order": 2
},
{
"_id": ObjectId("6a934e000102030405000003"),
"name": "Lyric 4",
"order": 2
},
{
"_id": ObjectId("6a934e000102030405000004"),
"name": "Lyric 5",
"order": 1
}
]
}
Output: (as you see lyrics are sorted by order field value)
[
{
"_id": ObjectId("5a934e000102030405000000"),
"createdAt": ISODate("2020-03-15T20:11:56.443Z"),
"lastEdited": ISODate("2020-03-18T20:11:56.443Z"),
"lyrics": [
[
{
"_id": ObjectId("6a934e000102030405000001"),
"name": "Lyric 2",
"order": 1
}
],
[
{
"_id": ObjectId("6a934e000102030405000002"),
"name": "Lyric 3",
"order": 2
}
],
[
{
"_id": ObjectId("6a934e000102030405000000"),
"name": "Lyric 1",
"order": 3
}
]
],
"setlistName": "list1"
},
{
"_id": ObjectId("5a934e000102030405000001"),
"createdAt": ISODate("2020-03-11T20:11:56.443Z"),
"lastEdited": ISODate("2020-03-11T20:11:56.443Z"),
"lyrics": [
[
{
"_id": ObjectId("6a934e000102030405000004"),
"name": "Lyric 5",
"order": 1
}
],
[
{
"_id": ObjectId("6a934e000102030405000003"),
"name": "Lyric 4",
"order": 2
}
]
],
"setlistName": "list2"
}
]