简体   繁体   English

如何将数据从 javascript 导入 mongoDB 数据库?

[英]How do I import data from javascript into a mongoDB database?

I am attempting to use a geolocation script that collects the location of the user.我正在尝试使用收集用户位置的地理定位脚本。 I'll also eventually add a geocoder but that can wait.我最终也会添加一个地理编码器,但可以等待。 This is the script:这是脚本:

<script>
        var x = document.getElementById("demo");
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            }
        }

        function showPosition(position) {
            x.innerHTML = "Latitude: " + position.coords.latitude +
            "<br>Longitude: " + position.coords.longitude;
        }

    </script>

My goal is to take the data that was collected about the user, and store it into a database using mongoDB where I can see it.我的目标是获取收集到的有关用户的数据,并使用 mongoDB 将其存储到可以看到的数据库中。 I am new to javascript but I know how to use mongoDB fairly well.我是 javascript 新手,但我知道如何很好地使用 mongoDB。 Can anybody help me?有谁能够帮助我?

you cannot do it without backend language like node js or php or something else.如果没有像 node js 或 php 或其他东西这样的后端语言,你就无法做到这一点。 You need to create api in backend language and then you can use frontend js to hit that api and get and store results.您需要使用后端语言创建 api,然后您可以使用前端 js 来访问该 api 并获取和存储结果。

As pointed out in answer above by @DHRUV GUPTA you need a backend and that is for both security as connecting to the db shouldnt be from the browser side and also to be able to use packages like mongoose .正如@DHRUV GUPTA 在上面的回答中指出的那样,您需要一个后端,这是为了安全性,因为连接到数据库不应该来自浏览器端,并且还可以使用像mongoose这样的包。 It is pretty similar in format to the mongoshell and shouldn't take too long to get used它在格式上与 mongoshell 非常相似,应该不会花太长时间才能使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM