How use $text $search with _ensureIndex and SimpleSchema in MeteorJs? - mongodb

I have no result in my search using $text $search with _ensureIndex and SimpleSchema in MeteorJs.
mycollection.js
var1: {
type: String
},
...
var2: {
type: new SimpleSchema({
_id: {
type: String
},
name: {
type: String
}
}),
},
search.js
results = myCollection.find({$text: {$search: searchText}});
this works
myCollection._ensureIndex({var1: 'text'});
With this, I have no result : why ?
myCollection._ensureIndex({var2: 'text'}); // here, I have no result
myCollection._ensureIndex({var2.name: 'text'}); // here, I have an error on console
Any idea ?
Thank you

If you want several fields to be searched in with the same query I'd suggest :
_ensureIndex({
"var1":"text",
"var2.name":"text"
})
I'd also make sure that all specified fields are Strings (typically if var2.name is a String, var2 isn't - not sure this is necessary depending on your schema but probably better)

try this:
App.Collections.Observations._ensureIndex({'var2.name': 1});

Related

What's the best way to update a Map/Dictionary which is part of a MongoDb document?

I'm new to MongoDb so I'm not sure what's the best approach regarding the following:
I have a MongoDb document which contains multiple fields, including a map/dictionary.
e.g. -> priceHistogram:
rents {
_id:"1234",
city:"London",
currentPrice:"500",
priceHistogram: {"14-02-2021" : "500"}
}
I would like to update the currentPrice field with the latest price but also add to the price histogram taday's date and the price'; e.g. if today's price would be 600, I would like to obtain the following:
rents {
_id:"1234",
city:"London",
currentPrice:"600",
priceHistogram: {"14-02-2021" : "500", "20-02-2021" ": "600"}
}
What would be the most efficient MongoDb function/approach allowing me to achieve this (everything else remains the same - _id/city)?
Thank you
Not sure how your schema looks like, I will assume that the schema looks similar to:
const rentsSchema = mongoose.Schema(
{
city: { type: String, required: true },
currentPrice: {type: String},
priceHistogram: {type: Map, of:String}
}
)
const rents = mongoose.model('Rents', histSchema);
And the update:
rents.updateOne({city:"London"},{
currentPrice:"600",
"priceHistogram.24-02-2021": "600"
})
Since as I have understood Map is another way to add arbitrary properties.

Argument passed in must be a single String of 12 bytes or a string of 24 hex characters, Mongoose ObjectId err

I actually searched a ton and I saw a ton of mentions of my problem here but none of the things I tried helped me fix the issue i'm having.
I have a Room Scheme that looks like this:
const ObjectId = mongoose.Schema.ObjectId;
const roomSchema = mongoose.Schema({
users: [{
type: ObjectId,
ref: 'User'
}],
messages: [{
type: ObjectId,
ref: 'Message',
}],
post: {
type: ObjectId,
ref: 'Post'
}
});
As you can see I have an array of users with ref to another schema Users
I'm trying to query all the Rooms that has a User ObjectId in it (search ObjectId in an array).
while I can easily get this with querying mongo from cmd using this:
db.users.find({users:ObjectId('THE_OBJECT_ID')});
when I try to get the same while using mongoose it fails with:
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
Here is how my route and find looks like:
app.route('/rooms/list/:user_id')
.get((req, res) => {
var query = { users: "USER_ID" };
Room.find(query ).populate('messages').then((data) => {
res.status(200).json(data);
}).catch((err) => {
console.log(err);
});
})
I tried to create type of object ID and use it but it still doesn't work.
var mongoose = require('mongoose'),
userId = 'THE_USER_ID';
var id = mongoose.Types.ObjectId(userId);
and than
Rooms.find({'users': id });
but it still doesn't work.
I also tried altering my query search using $in, $elemmatch it worked on cmd but failed when querying using mongoose.
Any help would be appreciated.
Issue :
If you check this :
var query = { users: "USER_ID" };
(Or)
userId = 'THE_USER_ID';
var id = mongoose.Types.ObjectId(userId);
What are you trying to do here ? You are passing in string USER_ID or THE_USER_ID as input and trying to convert it to type of ObjectId(). But string inside ObjectId() has certain restrictions which is why mongoose is failing to convert passed in string value to ObjectId() and getting error'd out.
Try this code :
Code :
const mongoose = require('mongoose');
app.route('/rooms/list/:user_id')
.get((req, res) => {
var query = { users: mongoose.Types.ObjectId(req.params.user_id) };
Room.find(query).populate('messages').then((data) => {
res.status(200).json(data);
}).catch((err) => {
console.log(err);
});
})
Your input should be value of user_id (Which will be string) - Convert it to ObjectId() and then query DB. So value of user_id should be a string that obeys ObjectId()'s restrictions, You can take string from one of existing doc's ObjectId() & test your get api.

Mongoose Mongodb

Thanks in advance.
I have a mongoose schema as below:
var bookSchema = new mongoose.Schema({
name: { type: String, index: true, default: '' },
text: { type: String, index: true, default: '' },
date: { type: Date, default: Date.now },
price: { type: Number, default: 0 } });
bookSchema.index({text: 1, name: 1}, {unique: true});
My problem is when I search for any text in the field named "text" it doesn't work. Possibly a naming conflict, do I have to change the field name to something other than text...
Book.find( { $text : { $search : 'mongoose is great' } } )...
text is a reserved word in MongoDB. Don't use this word, try with another.
Thanks for your effort in helping.
The issue I had was a small but painful lesson. As it turns out I went into my mLabs console and noticed that the Indexes for the collection totaled more than 16! So deleted all of them and added the one I needed. It turned out great, fixing my problem.
So I am guessing that if you keep adding Indexes without deleting the prior one's you end up getting unpredictable results.
Thanks again to all that answered.

Using Meteor.ObjectID as a type in SimpleSchema

I have two Schemas using SimpleSchema - say Schema1 and Schema2. I want to reference the internal ObjectId (_id) from Schema1 in Schema2. How do I go about doing this?
Schema1 looks like this:
Schema1 = new SimpleSchema({
aitem: {
type: String
},
anitem: {
type: String
}
});
Schema2 looks like this:
Schema2 = new SimpleSchema({
aaitem: {
type: String
},
aanitem: {
type: String
},
refItem: {
type: Mongo.ObjectID
}
});
When I try an insert in Meteor it says cannot validate ObjectID
Use: Mongo.Collection.ObjectID
Mongo.ObjectId is not an acceptable validator. The internal _id values are strings to JavaScript, so you can do: type: String and that should work.

Trouble updating a Simple Schema sub document

I'm trying to update a sub document on an existing collection. I'm getting a MongoDB error message.
"MongoError: The positional operator did not find the match needed from the query. Unexpanded update: articleWords.$ [409]"
From my Articles Simple Schema
"articleWords.$": {
type: Object
},
"articleWords.$.wordId": {
type: String,
label: 'Word ID'
},
"articleWords.$.word": {
type: String,
label: 'Word'
},
Update Function
function updateArticle(_id,wordArr) {
_.each(wordArr,function(elem) {
var ret = Articles.update(
{'_id': _id},
{ $set: { 'articleWords.$': { 'wordId': elem.wordId, 'word': elem.word } }
});
});
return true;
}
As you can see I am passing an array of objects. Is there a better way to do this than _.each ?
CLARIFICATION
Thank you to #corvid for the answer. I think I didn't make my question clear enough. There does exist an article record, but there is no data added to the articleWords attribute. Essentially we are updating a record but insert into the articleWords array.
A second attempt, is also not working
_.each(wordArr,function(elem) {
var ret = Articles.update(
{'_id': _id},
{ $set: { 'articleWords.$.wordId': elem.wordId, 'articleWords.$.word': elem.word } }
);
});
Yes, you need your selector to match something within the subdocument. For example,
Articles.update({
'_id': <someid>,
'words.wordId': <somewordid>
}, {
$set: {
'words.$.word': elem.word,
'words.$.wordId': elem.wordId
}
});
If the array doesn't exist yet then you're going about this in the hardest way possible. You can just set the entire array at one go:
var ret = Articles.update(
{'_id': _id},
{ $set: { articleWords: wordArr }}
);
I can see that wordArr already has the id and string. This will work as long as it doesn't have more content. If it does then you can just make a second version with the parts you want to keep.