$text based search using mongojs doesn't appear to work - javascript

I have a mongo collection where I need to combine q regex search with a $text search as well. Using Robo 3t this seems to work fine and I get want I want.
However when trying to get this running in my node app along with mongojs, the query wont work until I remove the $text.
var inRiverCode = req.body.RiverCode
var inSpecies = req.body.Species
var inMonth = req.body.Month
db.rodAvail.find({
//RiverCode: inRiverCode, $text: { $search: "as"}, WeekStartDisplay:
{$regex: inMonth}
$and : [ {$text: { $search: inSpecies }},
{RiverCode: inRiverCode, WeekStartDisplay: {$regex: inMonth}}]
}, function(err, rodAvailList) {
if (err) {
res.send(err);
console.log(err);
return;
}
});
Am I doing something silly like a mistype in my query or is $text search not supported in mongojs.
I did ask on the git page but its very quite so I expected a long wait for a reply.
https://github.com/mafintosh/mongojs/issues/369

Related

Reading parsed JSON into an array doesn't work?

I've been working on an application for a while and this particular feature is a part of a function which is supposed to read data from an api into an array so that I can display the contents on a webpage. Right now, I'm stuck here. Originally, I had a much longer section of code that wasn't working, but I've cut it down to a more specific problem: jsonBody.length returns 5, as expected, but articles.length returns 'undefined' and I don't understand why.
request(options, function(err, request, body) {
var jsonBody = JSON.parse(body);
var articles = new Article(jsonBody);
console.log(jsonBody.length);
console.log(articles.length);
res.render('news');
});
Thank you so much if you could help me understand. I'm not even entirely sure that I'm supposed to be using var articles at all? I can get the JSON to print to the console just find if I use jsonBody, but if I do so, I'm not sure how to utilize the contents on my 'news' page.
Here is the extended code in case you would like to see.
var marketNewsSchema = new mongoose.Schema({
datetime: String,
headline: String,
source: String,
url: String,
summary: String,
related: String,
Image: String
});
var Article = mongoose.model('MarketNews', marketNewsSchema);
app.get('/api/marketNews', function(req, res) {
var query = {
'symbol': req.body.id
};
var options = {
url: 'https://api.iextrading.com/1.0/stock/aapl/news/last/5',
method: 'GET',
qs: query
};
request(options, function(err, request, body) {
var jsonBody = JSON.parse(body);
var articles = new Article(jsonBody);
console.log(jsonBody.length);
console.log(articles.length);
res.render('news');
});
});
and the raw JSON object should be in this format:
[
{
"datetime": "2017-06-29T13:14:22-04:00",
"headline": "Voice Search Technology Creates A New Paradigm For Marketers",
"source": "Benzinga via QuoteMedia",
"url": "https://api.iextrading.com/1.0/stock/aapl/article/8348646549980454",
"summary": "<p>Voice search is likely to grow by leap and bounds, with technological advancements leading to better adoption and fueling the growth cycle, according to Lindsay Boyajian, <a href=\"http://loupventures.com/how-the-future-of-voice-search-affects-marketers-today/\">a guest contributor at Loup Ventu...",
"related": "AAPL,AMZN,GOOG,GOOGL,MSFT",
"image": "https://api.iextrading.com/1.0/stock/aapl/news-image/7594023985414148"
}
]
I think your problem is that new Article() is not an array, but you expect it to be one.
As far as I can see, Article is a mongoose schema - not an array.
So if your jsonBody is an array of articles, you might want to map over this array and generate individual articles for each object in the list.
I.e.:
var jsonBody = JSON.parse(body);
var articles = jsonBody.map(function(data) {
return new Article(data);
})
console.log(articles.length);

Javascript API for query in Elasticsearch

If I do below query in Kibana, results : "tim is a good boy" and I want to same thing using js library in eclipse.
GET nirv/_search
{
"query" : {
"term" : { "user" : "tim" }
}
}
I looked everywhere but did not find much of help. So can anyone provide a bit of code for a query to Elasticsearch using JS API in eclipse. What I mean is that I have a function result which provides me what elasticsearch return for particular query.
Are you using the javascript client library provided by Elasticsearch?
Their docs show examples on how to get started and perform a search with the client:
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'nirv'
});
client.search({
user: 'tim'
}).then(function (body) {
var hits = body.hits.hits; // "tim is a good boy" should be here *somewhere*
}, function (error) {
console.trace(error.message);
});

bookshelf.js where and has query at same time

I'm trying to return from our db a query where:
event_id = event.attributes.id
user_to = user.id
stripeCharge IS NOT NULL
I think I can do this use the has feature in bookshelf. I can't seem to get both the where & has going at the same time. Here is what my query that works with just where:
analyticsPayments.where({
event_id: event.attributes.id,
user_to: user.id
}).fetch().then(function(payment) {
console.log(payment);
}).catch(function(err) {
console.log(err);
res.status(500).json(err);});
I tried putting in after the where .has(stripeCharge) but it didn't like that.
Any idea how to achieve what I am trying to do, or how to have both a where and a has clause?
Thanks

Meteor User table value axtracting

How do I pick the email address value from meteor Mongo user table?
I have written below query to pick the element:
users=Meteor.users.find({},{emails:1})
This the code I have written to fetch the email address, but I don't know how much it's affecting performance in the code:
users = Meteor.users.find({})
users.forEach(function(key,option){
key.emails.forEach(function (key,option){
console.log(key.address)
});
});
In meteor, you should call:
users = Meteor.users.find({}, { fields: { emails: 1 } })
Reference in docs
EDIT
Please remember users is a cursor object. Cursor objects can be handled directly in templates, and must be the return of publications. You can't iterate a cursor directly in a javascript loop.
Example: (remember authorization in production publications)
Meteor.publish('user-emails', function() {
return Meteor.users.find({}, { fields: { emails: 1 } });
});
If you want to directly access the user instances, for example to iterate them in a javascript code, you need to fetch the cursor (reference in docs).
Example:
var users = Meteor.users.find({}, { fields: { emails: 1 } }).fetch();
Now users is an array of users. Feel free to iterate them.
Example (I'm using underscore.js):
var users = Meteor.users.find({}, { fields: { emails: 1 } }).fetch();
_.each(users, function(user) {
console.log(user.emails);
});
Now, if you need a vector only with emails, one on each index, you can pluck the emails from a fetched array with underscore.js (reference of pluck)
var emails = _.pluck(Meteor.users.find({}, { fields: { emails: 1 } }).fetch(), 'emails');
Hope it works :)
if its not working, dont forget to return
return users

How to find if a coordinate is within a polygon in mongoose for node js

I have tried many different attempts but have failed. Currently, my query doesn't fail but it doesn't provide me any results. I am using Mongoose v 3.8.9 which uses Mongodb v 1.4.
I think my coordinates are indexed properly, since it works for quering using .geoNear(). However, geoNear() only allows for a single coordinate and hence I can't specifty g
Here is a sample of it that runs but doesn't return any result. I have even changed one of the documents to contain the point exactly to one of the four points specified in geoJSONpolygon.
var geoJSONpolygon = { type: 'Polygon',coordinates:[[43.6582231,-79.3988945],[43.6583683,-79.3980648],[43.6583143,-79.3979845],[43.6584212,-79.3975422]] }
newLocation.find({}).where('pos').within().geometry(geoJSONpolygon).lean().exec(function(err,doc){
console.log(doc);
res.send(doc);
});
That's what I use, might help you.
Position.find(
{ geo :
{ $geoWithin : { $box :
[ [ box[0] , box[1] ] ,
[ box[2] , box[3] ] ] } }
}, function(err, response) {
if (err) return err;
console.log(response)
});
You can also set to debug and check the result:
mongoose.set('debug', true)

Categories

Resources