I try to search in my database and everythig work ok except search on array of strings.
The stored data looks like this:
{
"_id": "634d8af01f5ed9a5b6669e20",
"content": "Some example text",
"heading": "<h1">Domů</h1>",
"slug": "/home",
"title": "Home",
"keyWords": ["one", "two", "three"]
}
This is my Mongoose schema:
{
// _id
// createdAt
// updatedAt
heading: { type: String, required: true }, // <h1>
title: { type: String, required: true, maxLength: 20 }, // text in menu
slug: { type: String, required: true, unique: true },
coverImgPath: { type: String, required: true },
content: { type: String, required: true },
allowUserEditor: { type: Boolean, required: true },
keyWords: { type: [String] },
}
MySchema.index({heading: "text", title: "text", content: "text", keyWords: "text"});
I try to search like this and content or heading works well, but keyWords does not.
db.Article.article.find({
$text: {
$search: req.params.query,
$caseSensitive: false,
$diacriticSensitive: false
}
}).sort({ score: { $meta: "textScore" } }).limit(10).exec();
I tried change .index(keyWords: "text") to .index("keyWords":"text") and it also does not work.
Related
I have a mongoDb model defined as follows:-
var mongoose = require("mongoose");
const postModel = new mongoose.Schema({
postId: {
type: String,
unique: true,
required: true
},
authorId: {
type: String,
required: true
},
post: {
authorHandle: {
type: String,
required: true
},
heading: {
type: String,
required: true
},
message: {
type: String,
required: true
},
creationDate: {
type: Date,
required: true
},
image: { type: Array },
video: { type: Array },
comments: {
type: Array
}
}
});
module.exports = mongoose.model("postModel", postModel);
Now I have a sample value of a document of the above model, suppose:-
postId: "aaa",
authorId: "bbb",
post: {
authorHandle: "someone#123",
heading: "hello",
message: "post 1",
creationDate: "some creation date string(please ignore this is irrelevant to my question)",
image: [],
video: [],
comments: [
{ commentId: "1", message: "Something", createdAt: sometime },
{ commentId: "2", message: "Something else", createdAt: sometime2 },
{ commentId: "3", message: "Something other", createdAt: sometime3 },
]
}
Now say the user wants to update the comment with commentId 2 of this post with postId "aaa". My question is that what is the best way to use the findOneAndUpdate() method to solve this problem?
const PostModel = require("./models/PostModel"); //just importing the model that is defined above
//the below is happening inside a request handler in Node + Express
PostModel.findOneAndUpdate(
//what to do here
)
What I have tried is pulling out that whole object and replacing it with a new object with the new message. But that doesnt seem like a very efficient way. Any and all help is greatly appreciated!
You should write:
const updatedPost = await PostModel.findOneAndUpdate(
{ postId: 'aaa', 'post.comments.commentId': 2 },
{ 'post.comments.$.message': 'Updated message'},
{ new: true }
)
I try to create new collection with mongodb.
I wrote the MongoDB shcema, Nodejs+express code and everything work fine until I send parameters with Json inside them.
I was created this schema (and also more code under this schema) but its not work and I got error:
const partySchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
title: { type: String, required: true },
datetime: { type: Date, required: true },
location: {
scraping_location: { type: String, required: true },
display_location: { type: String, required: false },
city: { type: String, required: false },
adress: { type: String, required: false },
area: { type: String, required: false }
},
picture: { type: String, required: true },
url: { type: String, required: true },
affiliate_url: { type: String, required: true },
music_by: {type: [], required: false },
music_type: {type: [], required: false },
min_age_for_men: {type: Number, required: true },
min_age_for_women: {type: Number, required: true },
status_check: {type: Boolean, default: false },
tags: {
concept: { type: String, required: false },
free_text: { type: [], required: false }
},
producer_id: { type: String, required: false },
});
and the code that I wrote with nodejs+express is this:
const { title, datetime, location, picture, url, affiliate_url, music_by, music_type, min_age_for_men, min_age_for_women, tags, producer_id } = req.body;
const party = new Party({
_id: new mongoose.Types.ObjectId(),
title,
datetime,
location: { scarping_location: location.location_scarping },
picture,
url,
affiliate_url,
music_by,
music_type,
min_age_for_men,
min_age_for_women,
tags,
producer_id
});
party.save().then(() => {
res.status(200).json({
message: 'Create a new user'
})
}).catch(error => {
res.status(500).json({
error
})
});
when I send reqeust to the api with this body:
{
"title": "test",
"datetime": "2002-07-15T10:00:00+0300",
"location": {
"location_scarping": "new york"
},
"picture": "test.jpg",
"url": "https://google.com",
"affiliate_url": "https://google.com",
"min_age_for_men": 18,
"min_age_for_women": 16
}
I got this error:
{
"error": {
"errors": {
"location.scraping_location": {
"name": "ValidatorError",
"message": "Path `location.scraping_location` is required.",
"properties": {
"message": "Path `location.scraping_location` is required.",
"type": "required",
"path": "location.scraping_location"
},
"kind": "required",
"path": "location.scraping_location"
}
},
"_message": "Party validation failed",
"name": "ValidationError",
"message": "Party validation failed: location.scraping_location: Path `location.scraping_location` is required."
}
}
why its happend?
Looks like this is just a typo, from your schema:
scraping_location: { type: String, required: true }
You're trying to insert: location_scarping not location_scraping
"location_scarping": "new york"
Hopefully that fixes it.
I want to delete an element according to the value of "noteID" from the note array inside the project schema in mongo db.
Project Schema
endDate: {
type: String,
required: true,
default: Date.now().toString()
},
notes: {
type: [NoteSchema],
required: false
},
users: [{
type: String,
required: false
}]
Note Schema
const NoteSchema = mongoose.Schema({
noteID: {
type: String,
require: true
},
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
endDate: {
type: Date,
default: Date.now()
},
priority: {
type: String,
default: "low"
}
});
returning json with express
"endDate": "11.11.2021",
"notes": [
{
"endDate": "2022-02-05T08:02:18.166Z",
"priority": "low",
"_id": "61fe2f423667ad054b62f84f",
"noteID": "cb978ec0-7229-4253-9e36-eaaf85b72ae3",
"title": "test note title",
"description": "test note desc"
},
{
"endDate": "2022-02-05T08:02:18.166Z",
"priority": "medium",
"_id": "61fe2f593667ad054b62f851",
"noteID": "760fda9f-e453-4e9c-bc9f-161f38fd29c0",
"title": "test note title 2",
"description": "test note desc 2"
}
],
"users": [],
I want to delete an item from this notes directory using noteID.
Use update with $pull to remove the element from the array by noteID:
db.collection.update({},
{
$pull: {
notes: {
noteID: "cb978ec0-7229-4253-9e36-eaaf85b72ae3"
}
}
})
Explained:
The above query will remove all objects with nodeID: "cb978ec0-7229-4253-9e36-eaaf85b72ae3" from the array of objects notes[]. The operation apply to the first found document in collection. If you need to apply to all documents in collections you need to add the { multi:true } option
playground
Basically you can delete a schema item like that in mongoose.
YourColletionName.findOneAndDelete({ noteID: "cb978ec0-7229-4253-9e36-eaaf85b72ae3" },
function (err, data) {
if (err){
console.log(err)
}
else{
console.log("Removed: ", data);
}
});
If you want to delete with the noteID link on your page you can use req.params.noteID instead of cb978ec0-7229-4253-9e36-eaaf85b72ae3.
Resources:
https://www.geeksforgeeks.org/mongoose-findoneandremove-function/
https://mongoosejs.com/docs/api.html#model_Model.findOneAndDelete
const projectSchema = mongoose.Schema({
title: { type: String, required: true, trim: true },
projectId: { type: String, required: true, trim: true, unique: true },
company: { type: mongoose.Schema.Types.ObjectId, ref: 'Company', required: true },
description: { type: String, trim: true },
remark: { type: String, trim: true },
active: { type: Boolean, required: true },
participants: [{
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
active: { type: Boolean, required: true },
timeSpent: [{
date: { type: Date, required: true },
hour: { type: Number, required: true }
}]
}]
});
I've searched through a lot of SO questions, but none of them seems to be able to solve my problem. Sometimes I need to update one of the field from one participant, I want to update it in a way that when
//this data to apply update on the first element of the participants array
const partialParticipantUpdate = {
active: false
}
can be easily update to the sub document without rewriting all the other property. Basically like findByIdAndUpdate not findByIdAndReplace. I've tried aggregation but its only for reading data not writing data. I've also tried
let p = await Project.update({
"_id": mongoose.Types.ObjectId("5ee22f90e550c32194fb7a91"),
"participants._id": mongoose.Types.ObjectId("5ee4b5bd4fdcdb0c8813f5f5")
}, {
"$set": { "participants.0": partialParticipantUpdate } })
console.log(p);
However some of the participants property is removed after the update
{
"_id": { "$oid": "5ee22f90e550c32194fb7a91" },
"title": "Geo Chart Example",
"projectId": "myId",
"company": { "$oid": "5ee22ca46c7fc4358ced20a1" },
"description": "asd",
"remark": "re",
"active": true,
"participants": [{
"_id": { "$oid": "5ee4f37f4d8c234cc02d405a" },
"active": false,
"timeSpent": []
},
{
"_id": { "$oid": "5ee4c8a955ed7f23445a3cbc" },
"user": { "$oid": "5ee0bdc318998236706b5e5a" },
"active": true,
"timeSpent": []
}
],
"__v": 0
}
The update did change the first participant's "active" property from true to false, but it also lost some of the property like "user" and "timeSpent" array will get emptied as well
Finally I found the solution
const partialParticipantUpdate = {
active: false
}
let p = await Project.findById("5ee22f90e550c32194fb7a91");
let x = p.participants.id("5ee7224be4e4386084b87d3c");
x.set(partialParticipantUpdate);
let response = await p.save({ validateBeforeSave: false });
console.log(response);
The only downside is that I have to communicate with mongoose twice
I'm stuck at finding a solution for the following query.
1) a user can select many categories and subcategories.
2) the user can see all other users how are selected the same categories and subcategories within a certain radius.
Here is the Schema of the user
const userSchema = new Schema(
{
image: { type: String, default: 'NA' },
firstName: { type: String, default: 'first name' },
lastName: { type: String, default: 'last name' },
email: { type: String, lowercase: true, unique: true, trim: true },
password: { type: String, min: 6 },
gender: { type: String, emun: ['male','female','other'] },
about: { type: String, default: 'about you' },
address: {
zipCode: { type: Number, default: 000000 },
place: { type: String, default: 'place' },
street: { type: String, default: 'street' },
country: { type: String, default: 'Country' },
location: {
type: { type: String, default:'Point'},
coordinates: { type:[Number], index:'2dsphere', default:[0,0] }
}
},
interests: [
{
_id : false,
category: {
id: { type: Schema.Types.ObjectId, ref: 'Category' },
name: { type: String }
},
subCategory: [
{
_id : false,
id: { type: Schema.Types.ObjectId, ref: 'Subcategory' },
name: { type: String }
}
]
}
]
}
);
In my controller here is what I tried
homeData: async (req, res, next) => {
const limit = Number(req.params.limit);
const { latitude, longitude, minDistance, maxDistance } = getUserCurrentLocation(req);
const usersWithSameInterests = await User.aggregate([
{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [longitude, latitude]
},
"distanceField": "distance",
"minDistance": minDistance,
"maxDistance": maxDistance,
"spherical": true,
"query": { "location.type": "Point" }
}
},
{
"$match": { "interests": { "$elemMatch": {name: 'Sports'} }} // hard coded for testing
},
{ "$sort": { "distance": 1 } },
{ "$limit" : limit },
{
"$project": {
"_id": 1,
"image": 1,
"firstName":1,
"lastName":1,
"distance": 1,
"createdAt": 1
}
}
]);
return respondSuccess(res, null, {
newNotification: false,
usersWithSameInterests: usersWithSameInterests
});
},
The response i'm getting is
{
"success": true,
"message": "query was successfull",
"data": {
"newNotification": false,
"usersWithSameInterests": []
}
}
Sample categories and subcategories
Category: Sports
Subcategories: Cricket, Football, Hockey, Tennis
Category: Learning Languages
Subcategories: English, German, Spanish, Hindi
looking forward for much-needed help.
thank you.
It seems that you have a few mismatched columns.
On the $geonear pipeline, the line "query": { "location.type": "Point" } should be: 'query': {'address.location.type': 'Point'}.
And on the $match pipeline, the line { "interests": { "$elemMatch": {name: 'Sports'} } should be 'interests': { '$elemMatch:' {'category.name': 'Sports'} }
Edit:
To match multiple interests on the category and subcategory field, You can use the $in operator on the $match pipeline. Like this:
{
'interests.category.name': { $in: ['Sports'] },
'interests.subCategory.name': {$in: ['Soccer']}
}
It'll return anyone that have Sports in the category name, and Soccer on subcategory name.