filter/search function in meteor - search

I'm looking to implement a search/filter function. It will use a drop down bar as its inputs (so we the user chooses from a set of options).
How my system works now is this: There is a Meteor.users collection, and an Instruments collection. Basically, each user has a list of instruments he plays (for example, Bob plays piano and violin, Alice plays the piano and the flute). In this case, there are 4 objects in the Instruments collection:
1) type: Piano
owner: (bob's ID)
2) type: Violin
owner: (bob's ID)
3) type: Piano
owner: (Alice's ID)
4) type: Flute
owner: (Alice's ID)
So what I'm trying to do is, when I choose "piano" on my drop down list, and then click search, I want it to display Bob and Alice's profiles. I guess this should be done using Meteor's publish feature, but just not sure how to do it (I'm a beginner here).
For example, if how do I tell that specific publication to run when I hit search? I see examples of people setting publish rules to only publish the subset they are searching for, but how do we tell it when to happen? Do we use another template for that so that when we click "search", the URL path changes and thus a different template?
To be clear, my question is just how should I go about doing this. Thanks!

To make it easy, try to bind the instrument ID in Person's Collection as Array,
[{
_id: 'BoBId',
Instrumets: [PianoId, ViolinId],
},{
_id: 'AliceId',
Instrumets: [PianoId, ViolinId],
}];
now you could find any profile, who has the instruments
Person.find({
Instruments:{
$in:[InstrumentsId]
}
});

Related

How to store multiple Profile Pictures in NodeJS & Mongodb?

I'm new to mongodb, so pls go easy.
Let's say that I am creating a website where, in creating profile, users can provide multiple profile images which would be presented in a way of carousel later when other check their profile. Let's say the first picture user uploaded should be the first image in the carousel.
And user are allowed to alter the order of images showing by dragging forward or backward in editing session in order to decide which image to be showed first. The key is the order is editable
According to what I learnt so far, I have created a simple document structure to store user's information as below, where username and profile_image were stored.
But, in this way, I am stuck with one picture only. May I know how should I alter the table, so that it allow to store multiple images, as well as the order of images.
May I know what I should do to implement my original function? Is it should be done by making another table to store order?
When I'm saying the order is editable, I'm saying something like this https://i2.wp.com/www.techjunkie.com/wp-content/uploads/2020/11/4-2-scaled.jpg?resize=690%2C1401&ssl=1
[ { username: 'username_1',
profile_image: 'path_of_the_image',
},
{ username: 'username_2',
profile_image: 'path_of_the_image',
}
]

Looking for Mongoose Schema suggestions for individual models

I am in the process of developing a REACT app that would allow a user to find a plant based on their homes environment (long term goal). For my MVP I want to have the user create an account, name their home, add individual rooms with lighting conditions and add plants to each of those rooms. In the event the user does not create any rooms the plants would just sit under their home rather than a specific room by default.
I am struggling on how to design my models and am looking for some suggestions. This will only be my second project on my own so my experience is quite limited.
Currently I have the following models; (attached is a screenshot of each of these models)
plantScheme w/ commonName, scientificName, lightLevel, careLevel, waterReq, etc - This will be seeded with around 100 of the most common household plants to begin with and I intend to use the database to display varying plants on the site whether or not a user is logged in. I also want to link these plants to a specific user when the plant is added to their home.
userSchema w/ username, email, password, home { type: String, required: false, default: 'My Home' } (This begins with where I am open to suggestions. I am not sure if this is the most effective way of establishing the users home), rooms: [ Room.schema ] (pulls from a separate model), plants: [ UserPlant.schema ] (also pulls from a separate model to keep track of plant IDs and room IDs I THINK - haven't tested yet)
userPlantSchema w/ plant_id: { type: Schema.Types.ObjectId, ref: 'Plant', required: true } and room_id: { type: Schema.Types.ObjectId, ref: 'Room', required: false }
and lastly..
roomSchema w/ roomName and lightLevel
If there is a better way of setting up these models and/or keeping track of the users plants I would greatly appreciate the help. Thank you for your time.
screenshot of each model and their established relationships

Node.js Mongodb Two bookings on same time from two different users. only one can book

Currently in my application, users can make a booking for time XYZ, assuming that time XYZ is available. Suppose that another user is also booking for XYZ at the same time as the first user. I want to ensure that if they hit the particular API at the same time only one can have time XYZ and not both. Currently, when a user books a time, I first check my database to see if there is any booking for this particular time, and if there isn't the user gets that time. How can I make sure that if two users book at once, only one gets that time?
Create a unique index, something like this:
db.booking.createIndex( { "time": 1 }, { unique: true, sparse: true } )

Should I build one or two mongodb models?

I am building a mean stack app allowing people to read and write reviews about products.
Reviews will be classified by products on the website : A user will open the page displaying the list of products reviewed on the website and see the name of each products. When clicking on a product, the user is directed to a new page displaying all the reviews about the product.
I was set on building just one model gathering all reviews :
var ReviewSchema = new mongoose.Schema({
productName: String,
productCreator: String,
review: String,
upvotes: {type: Number, default: 0},
author: String
});
The page listing all products would get all reviews from the data base and then only display the productName and productCreator after eliminating all duplicates.
{{productName}} - {{productCreator}}
But it's probably not efficient at all. Would it be better to build two different models, one for the reviews and one for the products ?
I'm sorry if my question is stupid but I'm still learning about MEAN and don't know much about non-relational db, that's my first project ;)
I have built two models that I have embedded. It takes a little more time when writing all the rest routes, but it will simplify both the back and front in the end.

CouchDB a real world example

Tonight in my daily tech Googling I came across couchDB, after seeing tons of presentations about how it perform ten to hundred times better then any RDBM, how it would save us from SQL languages, tables, primary keys and so much more. I decided myself to try it myself. Only problem it seems I am unable to figure out how it works.
Like for a start I would like to code a web contact manager using couchDB. The project would enable user to do basic stuff like
Create/ Edit / Delete contacts
see a list of their contact ordered
search them on various criteria
So how do I start ?
Here some of my thoughts
create a database per user like July, Ann
in those DB, add some document with type contact, the document would look like this at first place see code 1
create / edit / delete is straight forward just need to do the PUT, POST, DELETE in the good database
searching would be handled by couchdb-lucene like dnolen suggested
now here come the difficult part, I don't really understand the whole map/reduce concept and how I can use that to do the jobs I used to do with SQL. Also with views how do you handle paging, also grouping.
I would like to build a screen with a paging set of links something like this
John, Doe
Johny, Hallyday
Jon, Skeet
A B C D E F **J** etc .... <-- those are link to see persons with that first name
what view should I create to achieve that, if you can provide samples it would wonderful.
Contact document.
{
type: 'contact',
firstname: 'firstname',
lastname: 'lastname',
email: ['home': 'foobar#foobar.net', 'work': 'foobar#foobar-working.net'],
phone: ['home': '+81 00 0000 0000'],
address: []
... some other fields maybe ...
}
The upcoming book by O'Reilly is free to read online:
http://books.couchdb.org/relax/
Just install and play around - you can do straight http requests using curl on the command line, or use the built-in web interface called futon.
Storing and retrieving data is really easy, the hardest part is thinking in terms of map/reduce-views instead of sql queries.
IBM has a great tutorial, making use of curl to read/write via the REST interface.
Your application is quite easy to do with CouchDB. You would have a database per user. Contacts are simply documents in a particular user's database. CRUD is just talking to the database using HTTP. You could create views that emit keys (last name, first name) to allow for sorting.
For powerful search I would recommend couchdb-lucene.

Resources