Mongoose access multiple collection - node.js

In mongodb I have one collection named as People.
It contains:
_id:Number,
name:String,
friends:[{friendsId:{type:Schema.Types.ObjectId,ref:'People'},addedTime: Date}]
I want to search a name of friend of a particular user and return the _id of that friend in a single query using mongoose in node.js.

Its impossible to retrieve the data in a single query/populate. Since mongodb is not a relational database. Iterate the data separately & give the result as callback to next query.

Related

findMany is not a function in MongoDB

I was making a commenting system on my website. I stored all the comment data in MongoDB and now I abviously need to access that data to print out the comments to a user. But, I see that there is no 'findMany' function for the collection object. I just need to get all the code in the collection in an array. findOne finds one document, then findMany should be there right? Is there any other such function in the collection object which can give me all the data in a collection of a database in MongoDB
Any help would be great!
You must use the find().toArray() method instead of findMany({ }) to get all the data there in the collection and to keep that data in an array. This will give you an array with all the data in the collection.

How to add timestamps to existing records in MongoDB

I am having a collection 'users' in MongoDB which contains multiple records without timestamps. I am using that collection with my node application and have set timestamps to true as shown:
const userSchema = new Schema({
...
},{
timestamps: true
});
I wanted to apply timestamps to the existing records and use it with my node application in future. If I make new fields 'createdAt' and 'updatedAt', will they work with my Mongoose schema? Or if there is any alternative way to achieve the task, please enlighten me as I am new to node and mongo in general.
first of all, I think this applies cannot affect the existing collections in the database, cause these fields are just a bunch of documents you inserting with existing/updating operations.
in MongoDB, everything is just a document and MongoDB does not care about data you store inside a collection, no validation here.so mongoose comes in for handling those validations and etc. if you change a schema in a collection it only effects to incoming requests from now on. but be careful if conflict fields happen, you will get an error for getting collections.
in short answer: MongoDB does not know when data stored or edited
but you can get timeStamp of creation in mongo ObjectId:
https://docs.mongodb.com/manual/reference/method/ObjectId.getTimestamp/index.html

query filter for mongodb using node js

I have two collection one is questions which stores _id, title, options, result, feedback and second is a child in the child I have store question_id, score. And I have filter the _id from questions collection. I don't know how I do this, Is it possible can we set the query for this. so that next time when I find the question from questions collection it sends filtered question. Means Return only that question from questions collection which id not same as the second collection child qustion_id.
This is my first collection where I have store questions, _id title option result feedback
_id:{type:String},
title:{type:String, required:true},
options:{type:Array, required:true},
result:{type:Array, required:true},
feedback:{type:String}
This is my Second collection where I have store attempted question_id and score
quiz:[
{
questionId:{
type:mongoose.Schema.Types.ObjectId,
ref: 'Question',
index: true
},
score:{type:Number},
time:{type:String}
}
]
This is not exactly I just create an example
var query = {}
firstcollection.find($and[{_id:},{secondcollection question_id:}]},function(err, data){
so that filter data means filter _id will store in data.
and I send this data to the frontend
res.send(data);
});
The main problem is conceptual, you are trying to work with mongodb, which is document store in RDBMS style. Under the community pressure Mondo added some minimal join functionality in latest version, but it doesn't make it relational DB.
There is no good way to perform such query. The idea behind document store is simple - you do have collection of documents and you query this collection, and only this collection. All link between collections are "virtual" and only provided by code logic, with no support from DB engine.
So all you can do with mongo is: query first collection for ids (with appropriate projection, to fetch ids only), store answer to some array and then perform second query to other collection using this array.

How to retrieve schema from a mongoose collection?

using this line :
var collection = mongoose.connection.db.collection("users");
I get users s collection, now i need to get the schema from that collection ?
Is there a way to do this? thank you.
PS: I dont have any models
if you do not have any models, I'm afraid this can't be done.
the schema is the structural representation of the document, not the collection. You can use the schema (via the model) to enforce that any document added to the collection match some structure, but there's nothing that prevents you from inserting into this collection a different document without this model, that will not match the criteria
so the schema does not live in the DB, it is a part of the mongoose module, and lives in the code. and the way to access it is through the model only

MongoDB _id field

I'm having trouble understanding with MongoDB is doing with my documents. I have a collection of a certain document. This document has a schema (I'm using Node.js Mongoose server-side). It has a couple of arrays of sub-documents. When I save a document that does not have an _id field Mongo should generate an _id field which it does. However, some of my 'sub-documents' also seem to have been given an _id field and other types of custom sub-documents were not. What the heck is going on here? Shouldn't I just get one _id per document?

Resources