Seed mongoDB with mongoose-seed - node.js

i am using mongoose-seed module to seed my database with some initial data, data is in json file and is not seeded in order from file
[{
"model": "User",
"documents": [
{
"name": "User1",
"email": "user1#gmail.com"
},
{
"name": "User2",
"email": "user2#gmail.com"
},
{
"name": "User3",
"email": "user3#gmail.com"
}
]
}]
In my db data is seeded in random order, not like user1, user2, user3.

Use Seedgoose, the ultimate seeder with smart reference support.

Related

How to find data based on sub-field name in MongoDB using Mongoose?

There is a sub-field called 'name' in MongoDB Collection (User):
[
{
"teacher": {
"name": "Alex",
"email": "alex#domain.com"
},
"waiter": {
"name": "Feliks",
"email": "feliks#domain.com"
},
"pilot": [
{
"name": "Sam",
"email": "sam#domain.com"
},
{
"name": "alice",
"email": "alice#domain.com"
}
],
},
{
"teacher": {
"name": "max",
"email": "max#domain.com"
},
"waiter": {
"name": "Sam",
"email": "sam#domain.com"
},
"pilot": [
{
"name": "richard",
"email": "richard#domain.com"
},
{
"name": "alice",
"email": "alice#domain.com"
}
],
}
]
How can I find data based on the field 'name'. For example, when I'm looking for 'Sam', it should return me the all documents since 'Sam' is in "waiter" and "pilot" in first and second documents respectively.
I cannot do something like:
User.find({"teacher.name": "Sam", "waiter.name": "Sam", "pilot.name": "Sam" })
This will return me nothing as it is an AND logic. What I need is an OR logic.
You can use the $or operator.
So the query should look like this:
User.find({ $or: [
{ "teacher.name": "Sam" },
{ "waiter.name": "Sam" },
{ "pilot.name": "Sam" }
]
});
Read here for me details.

NodeJs and Postman

I have 2 databases; First one is Users database where a registered user is an object with the 4 following keys (name, password, email and an array called "cats"). My next database is called Cats (again an object with 4 keys).
Now what I want to do is to update the User.cats each time the specific user uploads a cat in the Cats database. I have written all the requests for both databases (getUsers/getCats, postUser/postCat, putUser/putCat, patchUser/patchCat, deleteUser/deleteCat, getUserByID/GetCatById, getUserCount/getCatsCount);
I don't know how to access the users database so that User.cats gets updated whenever the user registers a new cat on his account.
These are the users:
{
"result": [
{
"name": "Razvan",
"email": "razvan#gmail.com",
"password": "Tricul",
"cats": []
},
{
"name": "mimisor",
"email": "mimi#hotmail.com",
"password": "MiMi",
"cats": []
},
{
"name": "Omar",
"email": "omar#gmail.com",
"password": "tataItz",
"cats": []
},
{
"name": "Dana",
"email": "danaraluca#gmail.com,",
"password": "MamaItz",
"cats": []
}
]
}
and these are the cats:
"result": [
{
"name": "Cocorico",
"age": "15 ani",
"breed": "Black Cat",
"owner": "UserId"
},
{
"name": "Mimi",
"age": "3 ani",
"breed": "Egyptian Mau",
"owner": "UserId"
},
{
"name": "Blondsky",
"age": "10 ani",
"breed": "Ginger Boy",
"owner": "UserId"
},
{
"name": "Billy",
"age": "1 ani",
"breed": "Aarabian Mau",
"owner": "UserId"
}
]
}
Basically you have two collections relationship.
I assume that you are using MongoDB. You can read about Mongoose to generate the component user and cat and use populate (I don't remember is MongoDB has something native to do that)
https://vegibit.com/mongoose-relationships-tutorial/
Other option for any DB, in your function you can read the users and do an iteration where for each user, you read from DB the cats that you have for that user and complete the field cats.

How to query all documents where array field have at least one object-element with property equal 'X'?

I have some MongoDB documents like that:
{
"group": "P32666",
"order": [{
"_id": {
"$oid": "5e8e9b40e7999f6b90fd88bf"
},
"name": "Dmitriy A",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "b6a19744-bb20-4d39-9e1e-0ca5b464f890"
}, {
"_id": {
"$oid": "5e8ea03f5a21c26b90983de4"
},
"name": "Dmitriy B",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "556924c3-605c-44cc-8a26-d32f58222e89"
}, {
"_id": {
"$oid": "5e8ea0645a21c26b90983de5"
},
"name": "Dmitriy C",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "aef00707-ef00-4ce9-918b-5cef17e7280b"
}]}
I'm working with Mongo in Mongoose and can't understand how to query all documents(like above) where field(array) "order" has at least one object within where field, for example, "login" is queal to "example". How can I do this?
I tried something like this:
export async function getQueues(request: Request, response: Response) {
const returningQueues = await queues.find({order: [login: request.params.login]});
response.json(returningQueues);
But TypeScript error (56 missing properties(mostly internal Mongoose's) of type "User", which I store within an array) says that's I am wrong in my thoughts.
If you just have one condition on the array field, you can simply do:
queues.find({"order.login": request.params.login})
But if you have multiple conditions, you can use $elemMatch:
queues.find({
order: { $elemMatch: { login: request.params.login, name: request.params.name } }
})

Tree structure in mongodb with parent child relation for read

I want to read data in tree structure with nested child of one parent, I am using mongodb. enter image description here
I want this type of expected data from the mongodb
[
{
"_id": "5d1fd59b1d26b42b2fdecf69",
"email": "admin#admin",
"userName": "admin",
"parrentId": "5d1fd9a6f612542b8d72337d",
"childs": [
{
"_id": "5d1fd61af612542b8d723376",
"email": "test1#test.com",
"userName": "test1",
"parrentId": "5d1fd59b1d26b42b2fdecf69",
"childs": [
{
"_id": "5d1fd91af612542b8d723379",
"email": "test2#test.com",
"userName": "test2",
"parrentId": "5d1fd59b1d26b42b2fdecf69",
"childs": []
},
{
"_id": "5d1fd91af612542b8d723379",
"email": "test2#test.com",
"userName": "test2",
"parrentId": "5d1fd59b1d26b42b2fdecf69",
"childs": []
},
]
}
]
}
]

How to Write search Query for Couchdb in views where the search object is in array?

I have a documents having the following data.
Document 1.
{
"_id": "7d7db310ff3acc857c7f301f67979de5",
"_rev": "1-3ed97634540c35292155ad40b99cafc1",
"categories": [
"Computer",
"Mobile",
"Automobile",
"Plumbing"
],
"location": [
"19.374636239520235",
"72.82339096069336"
],
"gender": "male",
"username": "ayushcshah",
"password": "sokdncx76483ynxwhakdkxfka37",
"email": "ayushcshah#gmail.com"
}
Document 2.
{
"_id": "8c499253bce69b992ebe795906fac3d3",
"_rev": "1-ce394be6ab3c79111344f026e1a3adcf",
"categories": [
"Automobile"
],
"location": [
"19.387757921807914",
"72.82626360654831"
],
"gender": "male",
"username": "smithabc",
"password": "adsadgq36eygdas2ygduabhs",
"email": "smithabc#gmail.com"
}
I need a VIEW where I would send key as any string and it would find a document where categories array contain the search string and it would send me related user's email address in value.
Example:
If I send key as "Mobile" the I would get respected username as value "ayushcshah#gmail.com"
key":"Mobile","value":"ayushcshah#gmail.com"
key":"Computer","value":"ayushcshah#gmail.com"
key":"Automobile","value":"ayushcshah#gmail.com"
key":"Automobile","value":"smithabc#gmail.com"
key":"Plumbing","value":"ayushcshah#gmail.com"
Following map function should be enough:
function (doc) {
if (!doc.categories) return;
for (var c in doc.categories) {
emit(doc.categories[c], doc.email);
}
}

Resources