NodeBB - Post Reputation/Likes - Find which users liked a post - node.js

I am not able to find where does NodeBB stores the list of users who liked a particular post.
For example, consider following data structure:-
> db.objects.find({_key:"post:2341"}).pretty()
{
"_id" : ObjectId("5547af3f65190fe2122d0b3c"),
"_key" : "post:2341",
"edited" : 0,
"pid" : 2341,
"content" : "content of this post",
"tid" : 2543,
"timestamp" : 1412304172707,
"deleted" : 0,
"editor" : "",
"uid" : 747,
"toPid" : 19999,
"votes" : 0,
"reputation" : 5
}
The above says that Post ID 2341 has 5 reputations which means it is liked by 5 users. But where does it stores that these are the User IDs who liked this particular post?

Finally hunted down for the exact key that stores it in the database via going through NodeBB code. And the particular key that stores it is pid:{postid}:upvote. So we query like this:-
>db.objects.find({_key: "pid:2341:upvote"})
{
"_id": ObjectId("5547af3f65190fe2122d0b3c"),
"_key": "pid:2341:upvote",
"members": ["663", "230", "549"]
}
The above response contains the IDs of the users who upvoted a particular post.

Related

mongoose, nodejs - add reference of current schema object to the previous schema object

I am using mongoose, nodejs with MVC architecture.
So, I have two collections crops and pesticides. I want a many to many relationship between these two collections.
For example, if I have 2 crops like below:
{
"_id" : ObjectId("5af1d1d54558fae1d0010bb4"),
"nameOfCrop" : "Tomato",
"imageOfCrop" : "tomatoimage",
"soilType" : " almost all soil types except heavy clay",
"waterNeeded" : "water once every two or three days",
"tagCrop" : "Vegetables",
"pesticideForCrop" : [ ]
}
{
"_id" : ObjectId("5af1d1d54558fae1d0010bb5"),
"nameOfCrop" : "Brinjal",
"imageOfCrop" : "brinjalimage",
"soilType" : "all types of soil varying from light sandy to heavy clay",
"waterNeeded" : "Regularly irrigated",
"tagCrop" : "Vegetables",
"pesticideForCrop" : [ ]
}
and two pesticides like below:
{
"_id" : ObjectId("5af7d3e735d4222b78a93838"),
"cropForPesticide" : [ ],
"nameOfPesticide" : "pesticide8",
"imageOfPesticide" : "p8image",
"__v" : 0
}
{
"_id" : ObjectId("5af7d49122b63e0824ed2d3d"),
"cropForPesticide" : [ ],
"nameOfPesticide" : "pesticide9",
"imageOfPesticide" : "p9image",
"__v" : 0
}
What I want is that tomato's pesticideForCrop key have object ids(suppose) of the pesticide pesticide8 and pesticide9 (meaning tomato can be treated with pesticide8 and pesticide9) and simultaneously I want a reference(_id) of tomato in the pesticide8's cropForPesticide key and pesticide9's cropForPesticide key.
I have a very vague approach in mind like firstly, I save a crop with the pesticideForCrop key being null at this point. Then I save a pesticide and while saving it, I can ask the user to select the crops which can be treated with that pesticide. I don't know how to code this. It would be nice if another feasible approach can be notified of or someone can point me in the right direction of how to code this.

how to prevent auto deletion collection records from Mongodb

Hello all I got stuck somewhere, I am working on mongodb with node.js where my collection data deleted automatically after 1 year on certain date and I want to stop that permanently how can I do that ? I have checked the available material on google but didn't got much success please help me friends ...
I have checked the index in one of my collection and it is showing data like this . Can you please tell me its is having TTL index or not
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "firstfive.teachers"
},
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "firstname_lastname_text",
"weights" : {
"firstName" : 1,
"lastName" : 1
},
"default_language" : "english",
"language_override" : "language",
"ns" : "firstfive.teachers",
"textIndexVersion" : 2
}
]
most likely you have TTL (time to limit) index defined on collection you're working with (https://docs.mongodb.com/v3.2/core/index-ttl/)
yu can check it by running db.your_collection.getIndexes() (it will be one with expireAfterSeconds) in mongo shell.
as any other index it can be removed - but do it carefully, apparently someone did it deliberately

Find documents with sub-documents matching both of two (or more) properties

In Node with Mongoose I want to find an object in the collection Content. It has a list of sub-documents called users which has the properties stream, user and added. I do this to get all documents with a certain user's _id property in there users.user field.
Content.find( { 'users.user': user._id } ).sort( { 'users.added': -1 } )
This seems to work (although I'm unsure if .sort is really working here. However, I want to match two fields, like this:
Content.find( { 'users.user': user._id, 'users.stream': stream } } ).sort( { 'users.added': -1 } )
That does not seem to work. What is the right way to do this?
Here is a sample document
{
"_id" : ObjectId("551c6b37859e51fb9e9fde83"),
"url" : "https://www.youtube.com/watch?v=f9v_XN7Wxh8",
"title" : "Playing Games in 360°",
"date" : "2015-03-10T00:19:53.000Z",
"author" : "Econael",
"description" : "Blinky is a proof of concept of enhanced peripheral vision in video games, showcasing different kinds of lens projections in Quake (a mod of Fisheye Quake, using the TyrQuake engine).\n\nDemo and additional info here:\nhttps://github.com/shaunlebron/blinky\n\nThanks to #shaunlebron for making this very interesting proof of concept!\n\nSubscribe: http://www.youtube.com/subscription_center?add_user=econaelgaming\nTwitter: https://twitter.com/EconaelGaming",
"duration" : 442,
"likes" : 516,
"dislikes" : 13,
"views" : 65568,
"users" : [
{
"user" : "54f6688c55407c0300b883f2",
"added" : 1427925815190,
"_id" : ObjectId("551c6b37859e51fb9e9fde84"),
"tags" : []
}
],
"images" : [
{
"hash" : "1ab544648d7dff6e15826cda7a170ddb",
"thumb" : "...",
"orig" : "..."
}
],
"tags" : [],
"__v" : 0
}
Use $elemMatch operator to specify multiple criteria on an array of embedded documents:
Content.find({"users": {$elemMatch: {"user": user.id, "stream": stream}}});

Querying a property that is in a deeply nested array

So I have this document within the course collection
{
"_id" : ObjectId("53580ff62e868947708073a9"),
"startDate" : ISODate("2014-04-23T19:08:32.401Z"),
"scoreId" : ObjectId("531f28fd495c533e5eaeb00b"),
"rewardId" : null,
"type" : "certificationCourse",
"description" : "This is a description",
"name" : "testingAutoSteps1",
"authorId" : ObjectId("532a121e518cf5402d5dc276"),
"steps" : [
{
"name" : "This is a step",
"description" : "This is a description",
"action" : "submitCategory",
"value" : "532368bc2ab8b9182716f339",
"statusId" : ObjectId("5357e26be86f746b68482c8a"),
"_id" : ObjectId("53580ff62e868947708073ac"),
"required" : true,
"quantity" : 1,
"userId" : [
ObjectId("53554b56e3a1e1dc17db903f")
]
},...
And I want to do is create a query that returns all courses that have a specific userId in the userId array that is in the steps array for a specific userId. I've tried using $elemMatch like so
Course.find({
"steps": {
"$elemMatch": {
"userId": {
"$elemMatch": "53554b56e3a1e1dc17db903f"
}
}
}
},
But It seems to be returning a empty document.
I think this will work for you, you have the syntax off a bit plus you need to use ObjectId():
db.Course.find({ steps : { $elemMatch: { userId:ObjectId("53554b56e3a1e1dc17db903f")} } })
The $elemMatch usage is not necessary unless you actually have compound sub-documents in that nested array element. And also is not necessary unless the value being referenced could possibly duplicate in another compound document.
Since this is an ObjectId we are talking about, then it's going to be unique, at least within this array. So just use the "dot-notation" form:
Course.find({
"steps.userId": ObjectId("53554b56e3a1e1dc17db903f")
},
Go back and look at the $elemMatch documentation. In this case, the direct "dot-notation" form is all you need

MongoDB collection scheme

I am starting to develop online football management game using NodeJS and MongoDB. But now i don't know, should i use multiple collections or can i put everything in one ? Example:
{
"_id" : ObjectId("5118ee01032016dc02000001"),
"country" : "Aruba",
"date" : "February 11th 2013, 3:11:29 pm",
"email" : "tadad#adadasdsd.com",
"name" : "test",
"pass" : "9WcFwIITRp0e82ca3c3b314a656bfb437553b1d013",
"team" : {
"name" : "teamname",
"logo" : "urltologo",
"color" : "color",
"players" : [{
"name" : "name",
"surname" : "surname",
"tackling" : 58,
"finishing" : 84,
"pace" : 51,
....
}, {
"name" : "name",
"surname" : "surname",
"start_age" : 19,
"tackling" : 58,
"finishing" : 84,
"pace" : 51,
...
}],
"stadium" : {
"name" : "stadium",
"capacity" : 50000,
"pic" : "http://urltopic",
....
},
},
}
or create different collections for users, fixtures, players, teams ? Or any other method ?
When I started with MongoDB, I went by the mantra of 'embed everything', which is exactly what you're doing above. However, there needs to be some consideration for sub-documents that can grow to be very large. You should think about how often you'll be updating a particular document or subdocument as well. For instance, your players are probably going to be updated on a regular basis, so you'd probably want to put them in their own collection for ease of use. Anyway, the flexibility of MongoDB makes it so that there's really no 'right' answer to this problem, but it may help you to refer to the docs on data modeling.
There is no hard and fast rule on how to design schemas in mongo. A lot depends on your application data access patterns, frequency of data access and the relationships between different entities, how they shrink/grow/change and which of them stay intact. It is not feasible to give an advice without knowing how your application is supposed to work. I recommend you consult a book, such as MongoDB in Action for example, which has advice on how to design schema in mongo properly taking into the account application specific requirements.

Resources