Mongodb-Rest queries with like [duplicate] - javascript

My question is simple but i can not find exact solution. All articles have gor below a line of code:
collection.findOne({hello:'world_no_safe'});
Above codes does work and returns to me undefined error.But it is existed. Anyway, My exact solution is above. Please help me and teach me how to use regex by searching inside of the json api. ı want to use %mysearch data% as a regex expression. ı want to make text search which is used all sentences in json.
For example : i have a json:
[{"data":"Andreas Kollegger explains what kinds of questions you can answer with a graph database. It's not only the big sites that "},{"data":"X explains what kinds of questions you can answer with a graph database. It's not only the big sites that "}]
if i use this expression: collection.findOne({hello:'%Andreas%'});
it has to return first data. Below a real sample from my project.
var mongo = require('mongodb');
var Server = mongo.Server;
var Db = mongo.Db;
var server = new Server('ds053479.mongolab.com', 53479, {auto_reconnect : true});
var db = new Db('orient', server);
db.open(function(err, client) {
client.authenticate('testname', 'fsfsff', function(err, success) {
var collection = db.collection('Models');
collection.insert({Name:'test1'});
// Fetch the document
collection.findOne({Name:'%world_no_safe%'});
});

According to the MongoDB Manual you can use the $regex operator:
collection.findOne({Name: { $regex: '.*world_no_safe.*' }});

Related

NLP Code Snippets NodeJS

I'm writing an application in NodeJS with the talkify library. The point of the application is to be a chatbot. Part of the functionality of the bot will be to take code (a closed source language) snippets, process them via a REST API, and return the result.
I've been able to create some trivial skills with talkify and it works fine. My main question is how would I implement my bot to interpret code snippets between single quotes in a given skill?
I'm not tied to talkify by any means. If someone has a suggestion that's better suited for my use case then I'm open to changing.
Code I have so far
// Core dependency
const talkify = require('talkify');
const Bot = talkify.Bot;
// Types dependencies
const BotTypes = talkify.BotTypes;
const Message = BotTypes.Message;
const SingleLineMessage = BotTypes.SingleLineMessage;
const MultiLineMessage = BotTypes.MultiLineMessage;
// Skills dependencies
const Skill = BotTypes.Skill;
// Training dependencies
const TrainingDocument = BotTypes.TrainingDocument;
const bot = new Bot();
bot.trainAll([
new TrainingDocument('how_many_endpoints', 'How many endpoints are there?'),
new TrainingDocument('how_many_endpoints', 'How many endpoints are there')
], function() {});
var howManyAction = function(context, request, response, next) {
response.message = new SingleLineMessage(function() {
var x = someWork();
return x;
});
next();
};
var howSkill = new Skill('how_skill', 'how_are_you', howAction);
bot.addSkill(howSkill);
var resolved = function(err, messages) {
if(err) return console.error(err);
return console.log(messages);
};
bot.resolve(123, TEXT_FROM_CHAT, resolved);
The above works when I ask how many endpoints I want but it won't work for more complex sentences, for example, I might want to say
"execute the following 'this is closed source stuff, I'll parse it server side'"
How would I match "execute the following" to a rule and get the string inside the single quotes as another variable? As I understand it the talkify library training documents must match the text it's parsing.
Example
Training document
execute the following
Text to match
execute the following <~ Match
execute the following 'foo bar' <~ No match
execute the following, <~ No match
You could pre-process the text to extract & remove any quoted phrase using a simple regex:
/\'[^\']*\'/g
See it running here: https://regex101.com/r/eUe7n3/1
hope it helps.

What is the correct "Type" to use for querying portfolio items using JavaScript

I'm trying to write what I thought would be a simple query to get one particular portfolio item from our workspace.
Here is my query code:
queryConfig = {
type : 'PortfolioItem',
key : 'stories',
query: '(Name = "Maintenance_Txn_Policy_Change")',
fetch: 'Name,Tags'
};
rallyDataSource = new rally.sdk.data.RallyDataSource('__WORKSPACE_OID__',
'__PROJECT_OID__',
'__PROJECT_SCOPING_UP__',
'__PROJECT_SCOPING_DOWN__');
rallyDataSource.findAll(queryConfig, showStories);
When I run this rallyDataSource comes back as undefined.
If I modify my code to look for userstories in stead:
queryConfig = {
type : 'hierarchicalrequirement',
key : 'stories',
query: '(Iteration.Name = "Iteration 2")',
fetch: 'Name,Tags'
};
rallyDataSource = new rally.sdk.data.RallyDataSource('__WORKSPACE_OID__',
'__PROJECT_OID__',
'__PROJECT_SCOPING_UP__',
'__PROJECT_SCOPING_DOWN__');
rallyDataSource.findAll(queryConfig, showStories);
I get correct answers.
So my quesiton is, what do I specify as the Type: in the query to get portfolio items, in particular, Features? I've tried some examples I've seen on this site (portfoilioitem, portfolioitem/Feature, etc.) but nothing seems to work.
You are using deprecated AppSDK1 that predates introduction of PortfolioItem objects in WS API. Use AppSDK2. See this example from AppSDK2 documentation that uses portfolioitem/feature.
There is a trick to make this work with AppSDK1 described in this SO post 2 years back. Note that the trick will only allow you to bump up the corresponding WS API version to 1.43, and not to the current 2.0.

Mongodb mapReduce: query another database

I'm trying to write a mapReduce function for MongoDb, and have a problem trying to query data from a second MongoDb database within the reduce function. Heres an example of the code:
var reduceUsers = function(key, values) {
var reducedUser = {
uid: key,
count: 0,
extractBytes: 0
};
var secondDb = db.getSiblingDB("second");
....
}
When I try running the mapReduce I get the following error:
015-09-04T15:50:23.690+0100 E QUERY Error: map reduce failed:{
"errmsg" : "exception: ReferenceError: db is not defined\n
All the code is in a .js file and I run it thus:
> mongo mapReduceTest.js
MapReduce can not access global shell objects like db. A MapReduce job can only obtain data from the one collection it runs on. It can not be used to aggregate data from more than one collection (good try, but MongoDB simply does not do JOINs). Whatever you are trying to do: it won't work this way.
When you would like advise how to solve the actual problem which lead you to this solution attempt, please open a new question which explains in detail what you are trying to do.

how to scrape content from a line break in a web pages using cheerio

Good day to you all.
I have 2 question on web-scraping using Cheerio. I went through the questions that might have my answer but could not find one that answer my question so I decided to ask a question.
Background info: I only learn Javascript for about 2~3 months so I might ask some really funny questions, please pardon me on that.
Objective: I'm looking to scrape data from the following site - and
I'm looking to get the
Name of bike store
Address of bike store
Telephone of bike shop
I've managed to scrape that data that I need, however they are lump in a html group (not sure if this is how to call it.
This is the code I used.
var request = require('request');
var cheerio = require('cheerio');
var url = 'http://www.togoparts.com/bikeshops/list_shops.php?country=MY';
request(url, function(err, resp, body) {
if (err)
throw err;
$ = cheerio.load(body, {
normalizeWhitespace: false
});
var doc = $("td[width='52%'].verdana1");
doc.each(function() {
var link = $(this);
console.log(link.html());
});
});
The result run in a loop and I'm able to get the following. I could not post an image - I have placed the image in the following link.
Question: How do I get the data separately?
I need the title of the link, I tried `var link = $(this).attr('href');' but does not work.
I also need the info (bikeshop address) after the line break - which I have no idea how to take it.
Question2:
I tried the following var doc = $("td[width='52%'] .verdana1"); - note the space before the .verdana1 - this give me only the title of the bikeshops I wanted, how is this different from var doc = $("td[width='52%'].verdana1");
and If I'm using this var doc = $("td[width='52%'] .verdana1"); - how can I get the data of the bike shop address?
Thank you so much for reading, I been trying to solve this during the Chinese New Year and it is driving me crazy :(. I looking forward to learn from you guys.
Bryan
In case of links you can first find them in your document, and then log all href attributes:
var doc = $("td[width='52%'].verdana1");
links = doc.find('a');
links.each(function (i,elem) {
console.log(elem.attribs.href);
})
This will log all values of href atttribute.
When it comes to addresses it's more complicated because they are not semantically distinguished in the DOM, they are also children of table cells, so you need nested loops, but you can access them by looking up element that are of type text.
doc.each(function (i,elem) {
# elem here is table cell
elem.children.forEach(function (child,i) {
# now all children of table cell (i.e links,spans,divs and just text)
if (child.type == "text") {
console.log(child.data);
}
})
});
Hope it helps.

Finding saved data from mongo shell (no output)

Here is the code for initialization
mongoose.connect('mongodb://localhost/gpsdb');
var db = mongoose.connection;
db.on('open', function () {
// now we can start talking
});
After successful opening, I am saving data like this, it's giving me no errors.
function saveGPSData(data){
var newData = new GPSData(data);
newData.save(function(err){
if(err)
return console.error(err);
});
}
Now in mongo shell, I am trying to retrieve that data but it's giving me empty output.
> use gpsdb
> db.GPSData.find();
>
It's giving me no output. Also can I found what models are there in gpsdb?
Here is the full source code http://pastebin.com/K7QPYAx8
JUST FOUND THAT in db folder there these files for my db created by mongodb
/data/db/gpsdb.0
/data/db/gpsdb.1
/data/db/gpsdb.n
A good place to start to get a quick answer is
https://groups.google.com/forum/#!forum/mongoose-orm
the community is very responsive :)
In the shell I did the following
>use gpsdb
switched to gpsdb
>db show collections
gpsdatas
From here I found that collection name is gpsdatas...... Not sure why its adding extra (s) to my modal, although you can see from the code that I am setting Modal to
var GPSData = mongoose.model('GPSData', GPSDataSchema);
Now using the shell its working like this
>db.gpsdatas.find()

Categories