how to find liked posts with user id - node.js

i am developing project using nodejs. I want to check if the user's userId is the same as the user of the post like. How should I be able to check ?
const audio = await Audio.find({likes: userId})
This is the post data i want to find
{
"title": "Điêu Toa",
"music": "public/audio/music_6149812c44cf4d33f410d680_1633941621172.mp3",
"singer": "Massew x Pháo",
"size": "2.72mb",
"date": {
"$date": "2021-10-11T08:37:42.840Z"
},
"owner": "6149812c44cf4d33f410d680",
"description": "Lời nói cứ như là bạc là vàng có ta và chàng ngồi ở đây chứng dám cho, chẳng việc gì phải lắng lo ~~",
"category": "Electronic",
"__v": 48,
"likes": [{
"user": "616c09216e4b2ff62f550c39"
}, {
"user": "6149812c44cf4d33f410d680"
}]
}

You could do it like that
const audio = await Audio.find({likes: {$elemMatch: {user: userId}}})

Related

Find a document by an object id in that doc in mongoDB node js express js

Hey this i want to fetch the products of a certain user so i added the user in the model by Object id , but when i was setting the router callBack function i didn't know how to do it !
{
"_id": {
"$oid": "62f250d3e2533cf4ca2ad7f3"
},
"image": "https://eshop-server.herokuapp.com/public/uploads/Surface-Pro-7-128GB-1604665289664.png",
"brand": "ASUS",
"price": {
"$numberInt": "250"
},
"rating": null,
"numReviews": null,
"isFeatured": false,
"name": "Surface Pro 7 128GB",
"description": "Tablet PC - Intel Core i3 1005G1 Ice Lake, touchscreen 12.3\" IPS 2736 × 1824, RAM 4GB LPDDR4X, Intel UHD Graphics",
"category": {
"$oid": "5f15d54cf3a046427a1c26e3"
},
"user": {
"$oid": "62f0e99be78e33cc5662d1f4"
},
"reviews": [{
"avatar": "https://pbs.twimg.com/profile_images/1760228143/Vetor-Johnny-Bravo-Vetorizado-Corel_400x400.jpg",
"name": "Johnny Bravo",
"review": "Amazing experience all around"
}, {
"avatar": "https://vignette.wikia.nocookie.net/asterix/images/3/3c/Asterix.png",
"name": "Asterix",
"review": "It's very hard. Only managed with resource to magic potion"
}, {
"avatar": "https://vignette.wikia.nocookie.net/looneytunes/images/7/7f/Coyote.gif",
"name": "Wild Coyote",
"review": "All my trickery and tools paid of. Thank ACME for suggesting this product to me"
}],
"countInStock": {
"$numberInt": "8"
},
"__v": {
"$numberInt": "0"
},
"richDescription": "eatures a detachable keyboard and offers the comfort of a classic laptop as well as the convenience of a tablet.&nbsp",
"images": ["http://localhost:3000/public/uploads/5f15d8852a025143f9593a7c-1604665316399.png", "http://localhost:3000/public/uploads/5f15d8852a025143f9593a7c-1604665316400.jpeg"]
}
as You can see the user object id is included so how can i find it ! this i what i tried but it didn't work
router.get(`/:id/products`, async (req, res) => {
const productList= await Product.find({user.$oid:req.params.id}).populate("user");
if (!productList) {
res.status(500).json({ success: false });
}
res.send(productList);
});
Solution
Replace Product.find({user.$oid:req.params.id})
with Product.find({user._id: mongo.ObjectId(req.params.id)})
Explanation
$oid is a way to represent ObjectId in JSON. $oid is not a property that exists on the document. We need to convert the id received in request to an ObjectId and match it with the _id field of the document.
Further
Wrap the conversion into a try/catch or something similar. Converting a string to an ObjectId can fail if the string is not a proper representation of an ObjectId.
const mongo = require('mongodb');
let idAsObjectId;
try {
idAsObjectId = mongo.ObjectId(req.params.id)
} catch (err) {
// handle error
}

How to sort for properties inside an other object in mongoose

I have this document and I want to sort the other documents based on the views.
{
"info": {
"title": "Hello",
"views": 500,
"cover": "https:----",
"duration": 203,
"album": true,
"lyrics": "---"
},
"_id": "62bb107c70a37ead49d685f8",
"userName": [
"Ben",
"Mike"
],
"audio": "---",
"__v": 0
},
This is my sorting code at the moment:
const song = await Song.find({}).sort({ views: 1 }).limit(10)
This code is not working I am getting back unordered documents.
Since the views field is inside info object, you have to use { "info.views": 1 }. This should give you the desired result.
const song = await Song.find({}).sort({ "info.views": 1 }).limit(10)

MongoDb find all objects that contain nested value

This is my user object sent as token in req:
{
"_id": "6212aba16653621e67393549c",
"name": "User",
"email": "user#gmail.com",
"__v": 0
}
This is my get function code:
const getSharedLists = asyncHandler(async (req, res) => {
const lists = await List.find({
sharedWith: { email: req.user.email },
});
res.status(200).json(lists);
});
This is what object looks like:
{
"_id": "621817233300dfff68e23710",
"user": "6212ab33383621e67393549c",
"listName": "test update",
"private": true,
"items": [
{
"itemName": "Bananas",
"quantity": 3,
"isBought": false,
"isle": "isle",
"_id": "621b043622147906eece2e72"
},
],
"sharedWith": [
{
"email": "user#gmail.com",
"_id": "621bdbf0791a322534284c49"
}
],
"createdAt": "2022-02-24T23:39:25.668Z",
"updatedAt": "2022-02-27T21:21:03.584Z",
"__v": 0,
},
I keep getting empty array back, even when hard code req.user.email as "user#gmail.com" for example. I need to find all lists on MongoDb that have my email in array of sharedWith.
Can somebody help please. Apparently I'm using List.find method wrong but can't seem to figure out the syntax.
You need (.) dot notation.
const lists = await List.find({
"sharedWith.email" : req.user.email
});
Sample Mongo Playground
Reference
Specify a Query Condition on a Field Embedded in an Array of Documents

How to update a value inside mongodb with nodeJS?

I'm trying to update a value inside mogoodb array but is the problem
database?
"TempChannels": [{
"user": "299481590445113345",
"channel": "794869948878159884",
"settings": []
}, {
"user": "583363766750806043",
"channel": "795004103998308352",
"settings": []
}],
The part of the code that should update the user:
Data.TempChannels[0].user = Target.id
Data.save().catch(er => console.log(er))
Also, no error appears when I run the code. and when i console the data it returns a user which has updated but it is not actually saved in mongodb!
code
Data.save().then(() => console.log(Data.TempChannels[0].user))
this is the whole data
{
"_id": {
"$oid": "5ff0cd1ee3d9fd2d40d82d23"
},
"TempChannels": [{
"user": "299481590445113345",
"channel": "795014692522295326",
"settings": []
}, {
"user": "583363766750806043",
"channel": "795015273060892753",
"settings": []
}],
"Guild": "704158258201624657",
"Stats": "true",
"ChannelID": "795014681664290826",
"ParentID": "795014680556994610",
"LogChannelID": "795014683601010709",
"TempControlChannelID": "795014682518749274",
"DefaultSettings": {
"limit": null,
"name": null,
"bitrate": null,
"copyperms": null
},
"__v": 2
}
I'm filtering the data by Guild
if you are using mongoose to connect MongoDB, the
Use markModified("updated field name")
Data.TempChannels[0].user = Target.id
Data.markModified('TempChannels');
Data.save().catch(er => console.log(er))
if you are using mongoose, there is a method that will allow to update the content of existing data
const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
res.n; // Number of documents matched
res.nModified; // Number of documents modified
If you are using Mongoose, you can update the record by doing something similar to this:
SchemaName.update({Guild: 'Guild Value Here'}, {$set: { "TempChannels[0].user" : "%newvalue%"}})
.then((data) => {
console.log(data)
})
.catch(err => {
console.log.error(err);
});
You should replace schemaName with the name of your schema if you are using mongoose, and in case you are not using it, I think it would be better for you to start using it.

Not able to query for nested relations using dgraph-orm

I am using dgraph-orm for fetching nested relational values but it works for single level but not multiple level.
I am getting the page details but unable to fetch the avatar of the page.
Here is my snippet:
let posts = await PagePost.has('page_id', {
filter: {
page_id: {
uid_in: [page_id]
}
},
include: {
page_id: {
as: 'page',
include: {
avatar: {
as: 'avatar'
}
}
},
owner_id: {
as: 'postedBy'
}
},
order: [], // accepts order like the above example
first: perPage, // accepts first
offset: offset, // accepts offset
});
I am not getting avatar for the attribute page_id:
{
"uid": "0x75b4",
"title": "",
"content": "haha",
"created_at": "2019-09-23T08:50:52.957Z",
"status": true,
"page": [
{
"uid": "0x75ac",
"name": "Saregamaapaaaa...",
"description": "This a is place where you can listen ti thrilling music.",
"created_at": "2019-09-23T06:46:50.756Z",
"status": true
}
],
"postedBy": [
{
"uid": "0x3",
"first_name": "Mohit",
"last_name": "Talwar",
"created_at": "2019-07-11T11:37:33.853Z",
"status": true
}
]
}
Is there a support for multilevel field querying in the orm??
There was some issue with ORM itself it was not able to recognize the correct model name for multilevel includes and generating the wrong queries.
Fixed the same in version 1.2.4, please run npm update dgraph-orm --save to update your DgraphORM.
Thanks for the issue.

Resources